Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't get connect to work
    primarykey
    data
    text
    <p>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.</p> <p>Here is my server code (setup).</p> <pre><code>addrinfo hints; addrinfo* pAddrInfo; char pIPAddress[INET6_ADDRSTRLEN]; char pPortFinal[128]; unsigned long tempUL; int error; memset(&amp;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, &amp;hints, &amp;pAddrInfo); if(error != 0) { GetWinSockError(error); return; } for(pAddrInfo = pAddrInfo; pAddrInfo != NULL; pAddrInfo = pAddrInfo-&gt;ai_next) { this-&gt;m_Socket = socket(pAddrInfo-&gt;ai_family, pAddrInfo-&gt;ai_socktype, pAddrInfo-&gt;ai_protocol); if(bind(this-&gt;m_Socket, pAddrInfo-&gt;ai_addr, pAddrInfo-&gt;ai_addrlen) != 0) continue; break; } if(!pAddrInfo) { GetWinSockError(WSAGetLastError()); freeaddrinfo(pAddrInfo); this-&gt;ShutDown(); return; } //7 = magic number recommended by 'beej's guide' online if(listen(this-&gt;m_Socket, 7) != 0) { GetWinSockError(WSAGetLastError()); freeaddrinfo(pAddrInfo); this-&gt;ShutDown(); return; } getnameinfo(pAddrInfo-&gt;ai_addr, pAddrInfo-&gt;ai_addrlen, pIPAddress, sizeof pIPAddress, pPortFinal, sizeof pPortFinal, NI_NUMERICHOST | NI_NUMERICSERV); this-&gt;m_pIPAddress = Helpers::String::NewCopy(pIPAddress); this-&gt;m_pPort = Helpers::String::NewCopy(pPortFinal); cout &lt;&lt; "IP:" &lt;&lt; this-&gt;m_pIPAddress &lt;&lt; " Port:" &lt;&lt; this-&gt;m_pPort &lt;&lt; "\n"; this-&gt;SetState(NET_COMM_STATE__READY); freeaddrinfo(pAddrInfo); </code></pre> <p>Here is my server code (tick).</p> <pre><code>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-&gt;m_Socket, (struct sockaddr*)(&amp;their_addr), &amp;addr_size); if(newClientSocket == -1) { if(WSAGetLastError() != WSAEWOULDBLOCK) GetWinSockError(WSAGetLastError()); return; } pSockAddr = (struct sockaddr*)(&amp;their_addr); pNetComm = new NetServerClient(newClientSocket); addr_size = sizeof their_addr; if(bind(newClientSocket, pSockAddr, addr_size) != 0) { GetWinSockError(WSAGetLastError()); pNetComm-&gt;ShutDown(); return; } if (pSockAddr-&gt;sa_family == AF_INET) { pTempBuffer = &amp;(((struct sockaddr_in*)pSockAddr)-&gt;sin_addr); port = ((struct sockaddr_in*)pSockAddr)-&gt;sin_port; } else { pTempBuffer = &amp;(((struct sockaddr_in6*)pSockAddr)-&gt;sin6_addr); port = ((struct sockaddr_in6*)pSockAddr)-&gt;sin6_port; } inet_ntop(their_addr.ss_family, pTempBuffer, pIPAddress, sizeof pIPAddress); _snprintf(pPort, 256, "%d", (unsigned int)port); pNetComm-&gt;StartCommunication(pIPAddress, pPort); this-&gt;m_vpClients.push_back(pNetComm); </code></pre> <p>And here is my client code:</p> <pre><code>addrinfo hints; addrinfo* pAddrInfo; unsigned long tempUL; int error; memset(&amp;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, &amp;hints, &amp;pAddrInfo); if(error != 0) { GetWinSockError(error); return; } for(pAddrInfo = pAddrInfo; pAddrInfo != NULL; pAddrInfo = pAddrInfo-&gt;ai_next) { this-&gt;m_Socket = socket(pAddrInfo-&gt;ai_family, pAddrInfo-&gt;ai_socktype, pAddrInfo-&gt;ai_protocol); if(connect(this-&gt;m_Socket, pAddrInfo-&gt;ai_addr, pAddrInfo-&gt;ai_addrlen) != 0) continue; break; } if(!pAddrInfo) { GetWinSockError(WSAGetLastError()); freeaddrinfo(pAddrInfo); this-&gt;ShutDown(); return; } this-&gt;SetState(NET_COMM_STATE__READY); freeaddrinfo(pAddrInfo); </code></pre> <p>The problem is that connect on the client always fails. I get the error "WSAECONNREFUSED: Connection refused".</p> <p>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.</p> <p>I have read these posts <a href="https://stackoverflow.com/questions/7942969/getting-server-ip-0-0-0-00-from-getaddrinfo">Getting server ip 0.0.0.0:0 from getaddrinfo()</a> and <a href="https://stackoverflow.com/questions/7558001/sin-port-returned-in-getaddrinfo-wrong">sin_port returned in getaddrinfo wrong</a> already and tried their solutions, but they didn't work for me.</p> <p>Any help would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload