1

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.

Community
  • 1
  • 1
FunnerSoft
  • 77
  • 8
  • Why are you `bind()`-ing each client socket? It's already bound when you get it from `accept()`? – Nikolai Fetissov Oct 06 '12 at 23:53
  • Nikolai, I guessed that the new socket would have to be bound in order to receive messages. I would not be surprised if that was wrong code, I have not been able to test anything past the accept call :( – FunnerSoft Oct 07 '12 at 00:36
  • You might try using telnet to try and connect to your server's port. If telnet can connect, then you know for sure the program is on the client side; if not, then there's a server-side problem. – Jeremy Friesner Oct 07 '12 at 02:23
  • What values are you passing to `getaddrinfo()` in `pIPAddress` and `pPort` on the client? – Nikolai Fetissov Oct 07 '12 at 02:51

0 Answers0