This commit is contained in:
Benjamin Sergeant 2020-01-27 17:29:44 -08:00
parent 86c2ab450e
commit e927dcca3a

View File

@ -59,7 +59,12 @@ namespace ix
std::pair<bool, std::string> SocketServer::listen() std::pair<bool, std::string> SocketServer::listen()
{ {
struct sockaddr_in server; // server address information if (_addressFamily != AF_INET && _addressFamily != AF_INET6)
{
std::string errMsg("SocketServer::listen() AF_INET and AF_INET6 are currently "
"the only supported address families");
return std::make_pair(false, errMsg);
}
// Get a socket for accepting connections. // Get a socket for accepting connections.
if ((_serverFd = socket(_addressFamily, SOCK_STREAM, 0)) < 0) if ((_serverFd = socket(_addressFamily, SOCK_STREAM, 0)) < 0)
@ -82,28 +87,59 @@ namespace ix
return std::make_pair(false, ss.str()); return std::make_pair(false, ss.str());
} }
// Bind the socket to the server address. if (_addressFamily == AF_INET)
server.sin_family = _addressFamily;
server.sin_port = htons(_port);
if (inet_pton(_addressFamily, _host.c_str(), &server.sin_addr.s_addr) <= 0)
{ {
std::stringstream ss; struct sockaddr_in server;
ss << "SocketServer::listen() error calling inet_pton " server.sin_family = _addressFamily;
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno()); server.sin_port = htons(_port);
Socket::closeSocket(_serverFd); if (inet_pton(_addressFamily, _host.c_str(), &server.sin_addr.s_addr) <= 0)
return std::make_pair(false, ss.str()); {
std::stringstream ss;
ss << "SocketServer::listen() error calling inet_pton "
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
Socket::closeSocket(_serverFd);
return std::make_pair(false, ss.str());
}
// Bind the socket to the server address.
if (bind(_serverFd, (struct sockaddr*) &server, sizeof(server)) < 0)
{
std::stringstream ss;
ss << "SocketServer::listen() error calling bind "
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
Socket::closeSocket(_serverFd);
return std::make_pair(false, ss.str());
}
} }
else // AF_INET6
if (bind(_serverFd, (struct sockaddr*) &server, sizeof(server)) < 0)
{ {
std::stringstream ss; struct sockaddr_in6 server;
ss << "SocketServer::listen() error calling bind " server.sin6_family = _addressFamily;
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno()); server.sin6_port = htons(_port);
Socket::closeSocket(_serverFd); if (inet_pton(_addressFamily, _host.c_str(), &server.sin6_addr) <= 0)
return std::make_pair(false, ss.str()); {
std::stringstream ss;
ss << "SocketServer::listen() error calling inet_pton "
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
Socket::closeSocket(_serverFd);
return std::make_pair(false, ss.str());
}
// Bind the socket to the server address.
if (bind(_serverFd, (struct sockaddr*) &server, sizeof(server)) < 0)
{
std::stringstream ss;
ss << "SocketServer::listen() error calling bind "
<< "at address " << _host << ":" << _port << " : " << strerror(Socket::getErrno());
Socket::closeSocket(_serverFd);
return std::make_pair(false, ss.str());
}
} }
// //