0

When I try to connect to my server using localhost IP or my LAN IP, everything works fine and the client successfully connects.

Problems begin once I attempt to connect to the server using my external IP - I get just WSAETIMEDOUT error then and the server can't see any incoming connections.

Canyouseeme.org returns:

Error: I could not see your service on <my IP> on port (12345)
Reason: Connection timed out

The same thing happens when I try to ping my external IP - I get only Request timed out.

I disabled my firewall, my router's firewall, added NAT rule, but I still get the same errors.

My client's code (shortened):

SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(serverSocket == INVALID_SOCKET)
{
    printf("socket() failed with code %d\n", WSAGetLastError());

    return;
}

ZeroMemory(&serverSockAddrIn, sizeof(serverSockAddrIn));

serverSockAddrIn.sin_family = AF_INET;
inet_pton(AF_INET, "<My external IP>", &serverSockAddrIn.sin_addr.S_un.S_addr);
serverSockAddrIn.sin_port = htons(12345);

if(connect(serverSocket, (sockaddr*)&serverSockAddrIn, sizeof(serverSockAddrIn)) == SOCKET_ERROR)
{
    printf("connect() failed with code %d\n", WSAGetLastError());

    closesocket(serverSocket);

    return;
}

And my server's code (also shortened):

SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if(serverSocket == INVALID_SOCKET)
{
   printf("socket() failed with code %d\n", WSAGetLastError());

   return;
}

sockaddr_in serverSockAddrIn;

ZeroMemory(&serverSockAddrIn, sizeof(serverSockAddrIn));

serverSockAddrIn.sin_family = AF_INET;
serverSockAddrIn.sin_port = htons(12345);
serverSockAddrIn.sin_addr.S_un.S_addr = INADDR_ANY;

if(bind(serverSocket, (sockaddr*)&serverSockAddrIn, sizeof(serverSockAddrIn)) == SOCKET_ERROR)
{
   printf("bind() failed with code %d\n", WSAGetLastError());

   closesocket(serverSocket);

   return;
}

if(listen(serverSocket, SOMAXCONN) == SOCKET_ERROR)
{
   printf("listen() failed with code %d\n", WSAGetLastError());

   closesocket(serverSocket);

   return;
}

SOCKET clientSocket;

while((clientSocket = accept(serverSocket, NULL, NULL)) == INVALID_SOCKET)
   printf("accept() failed with code %d\n", WSAGetLastError());

printf("Client connected\n");

Regardless of whether the server is running or not, client's connect() keeps returning 10060 => WSAETIMEDOUT.

Server calls accept() and blocks, since client can't connect.

Note: I run both applications on the same machine, if it matters.

Have you any idea what did I do wrong?

Jason
  • 33
  • 1
  • 6
  • You clearly have a networking issue rather than a coding issue (the code you showed is fine). Your client simply does not have a route to reach the external IP. You should ask about that on [Server Fault](https://serverfault.com) instead. – Remy Lebeau Feb 05 '19 at 21:05
  • @RemyLebeau Thanks for your reply. I also think there's nothing wrong with my code, but it's always better to ask other people, as I couldn't get it to work anyhow. Shall I link this question on Server Fault or just copy it (maybe without the code part, though some people may want to actually see it, thinking it's my fault)? – Jason Feb 05 '19 at 22:07
  • A link to this question for reference will suffice, since your new question shoud be about the network setup and not the coding – Remy Lebeau Feb 05 '19 at 23:06

0 Answers0