I am working on some networking code. I have 2 solutions running on my same machine. The first is my server and the other is my client. I am using C++ and winsock for the networking solution.
Here is my server code (setup).
addrinfo hints;
addrinfo* pAddrInfo;
char pIPAddress[INET6_ADDRSTRLEN];
char pPortFinal[128];
unsigned long tempUL;
int error;
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_UNSPEC;//AF_INET; // don't care IPv4 or IPv6
hints.ai_socktype = type;
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
error = getaddrinfo(NULL/*"localhost"*/, pPort, &hints, &pAddrInfo);
if(error != 0)
{
GetWinSockError(error);
return;
}
for(pAddrInfo = pAddrInfo; pAddrInfo != NULL; pAddrInfo = pAddrInfo->ai_next)
{
this->m_Socket = socket(pAddrInfo->ai_family, pAddrInfo->ai_socktype, pAddrInfo->ai_protocol);
if(bind(this->m_Socket, pAddrInfo->ai_addr, pAddrInfo->ai_addrlen) != 0)
continue;
break;
}
if(!pAddrInfo)
{
GetWinSockError(WSAGetLastError());
freeaddrinfo(pAddrInfo);
this->ShutDown();
return;
}
//7 = magic number recommended by 'beej's guide' online
if(listen(this->m_Socket, 7) != 0)
{
GetWinSockError(WSAGetLastError());
freeaddrinfo(pAddrInfo);
this->ShutDown();
return;
}
getnameinfo(pAddrInfo->ai_addr, pAddrInfo->ai_addrlen, pIPAddress, sizeof pIPAddress, pPortFinal, sizeof pPortFinal, NI_NUMERICHOST | NI_NUMERICSERV);
this->m_pIPAddress = Helpers::String::NewCopy(pIPAddress);
this->m_pPort = Helpers::String::NewCopy(pPortFinal);
cout << "IP:" << this->m_pIPAddress << " Port:" << this->m_pPort << "\n";
this->SetState(NET_COMM_STATE__READY);
freeaddrinfo(pAddrInfo);
Here is my server code (tick).
NetServerClient* pNetComm;
sockaddr* pSockAddr;
sockaddr_storage their_addr;
socklen_t addr_size;
void* pTempBuffer;
char pIPAddress[INET6_ADDRSTRLEN];
char pPort[256];
unsigned short port;
int newClientSocket;
addr_size = sizeof their_addr;
newClientSocket = accept(this->m_Socket, (struct sockaddr*)(&their_addr), &addr_size);
if(newClientSocket == -1)
{
if(WSAGetLastError() != WSAEWOULDBLOCK)
GetWinSockError(WSAGetLastError());
return;
}
pSockAddr = (struct sockaddr*)(&their_addr);
pNetComm = new NetServerClient(newClientSocket);
addr_size = sizeof their_addr;
if(bind(newClientSocket, pSockAddr, addr_size) != 0)
{
GetWinSockError(WSAGetLastError());
pNetComm->ShutDown();
return;
}
if (pSockAddr->sa_family == AF_INET)
{
pTempBuffer = &(((struct sockaddr_in*)pSockAddr)->sin_addr);
port = ((struct sockaddr_in*)pSockAddr)->sin_port;
}
else
{
pTempBuffer = &(((struct sockaddr_in6*)pSockAddr)->sin6_addr);
port = ((struct sockaddr_in6*)pSockAddr)->sin6_port;
}
inet_ntop(their_addr.ss_family, pTempBuffer, pIPAddress, sizeof pIPAddress);
_snprintf(pPort, 256, "%d", (unsigned int)port);
pNetComm->StartCommunication(pIPAddress, pPort);
this->m_vpClients.push_back(pNetComm);
And here is my client code:
addrinfo hints;
addrinfo* pAddrInfo;
unsigned long tempUL;
int error;
memset(&hints, 0, sizeof hints); // make sure the struct is empty
hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6
hints.ai_socktype = type;
error = getaddrinfo(pIPAddress, pPort, &hints, &pAddrInfo);
if(error != 0)
{
GetWinSockError(error);
return;
}
for(pAddrInfo = pAddrInfo; pAddrInfo != NULL; pAddrInfo = pAddrInfo->ai_next)
{
this->m_Socket = socket(pAddrInfo->ai_family, pAddrInfo->ai_socktype, pAddrInfo->ai_protocol);
if(connect(this->m_Socket, pAddrInfo->ai_addr, pAddrInfo->ai_addrlen) != 0)
continue;
break;
}
if(!pAddrInfo)
{
GetWinSockError(WSAGetLastError());
freeaddrinfo(pAddrInfo);
this->ShutDown();
return;
}
this->SetState(NET_COMM_STATE__READY);
freeaddrinfo(pAddrInfo);
The problem is that connect on the client always fails. I get the error "WSAECONNREFUSED: Connection refused".
I am pretty certain the reason my connection fails is because my client is not passing the correct IP address to getaddrinfo. When I run my server, I output the IP and port of the server solution. Then I have my client solution call it's getaddrinfo with the same IP and port info. However, my server solution does not appear to be gathering the correct IP and port info. It always tell me it's IP is either 0.0.0.0 (IPv4) or :: (IPv6). I don't understand how I get the proper IP for my client to connect to my server.
I have read these posts Getting server ip 0.0.0.0:0 from getaddrinfo() and sin_port returned in getaddrinfo wrong already and tried their solutions, but they didn't work for me.
Any help would be much appreciated.