none
Unable to connect to a winsock server using external IP RRS feed

  • Question

  • 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 callsaccept() 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

    Friday, February 1, 2019 3:25 PM

All replies

  • Sorry about some typos I did, but I can't edit my question, as it's being "flagged as spam"...
    Friday, February 1, 2019 3:38 PM