c++windowssocketswinsock

winsock: connect fails with error 10049 when using localhost (127.0.0.1)


I wrote a class encapsulating some of the winsock functions to imitate a simple TCP socket for my needs...

When I try to run a simple connect-and-send-data-to-server test the "client" fails on its call to connect with the error code of 10049 (WSAEADDRNOTAVAIL) connect function on MSDN

What I am doing is (code below): Server:

  1. Create a Server Socket -> Bind it to Port 12345
  2. Put the Socket in listen mode
  3. Call accept

Client

  1. Create a socket -> Bind it to a random port
  2. Call Connect: connect to localhost, port 12345

=> the call to connect fails with Error 10049, as described above

Here is the main function including the "server":

HANDLE hThread = NULL;
Inc::CSocketTCP ServerSock;
Inc::CSocketTCP ClientSock;

try
{

    ServerSock.Bind(L"", L"12345");
    ServerSock.Listen(10);

    //Spawn the senders-thread
    hThread = (HANDLE)_beginthreadex(nullptr, 0, Procy, nullptr, 0, nullptr);

    //accept
    ServerSock.Accept(ClientSock);


    //Adjust the maximum packet size
    ClientSock.SetPacketSize(100);


    //receive data
    std::wstring Data;
    ClientSock.Receive(Data);

    std::wcout << "Received:\t" << Data << std::endl;
}
catch(std::exception& e)
{...

Client thread function

unsigned int WINAPI Procy(void* p)

{

Sleep(1500);
try{
    Inc::CSocketTCP SenderSock;
    SenderSock.Bind(L"", L"123456");

    SenderSock.Connect(L"localhost", L"12345");


    //Adjust packet size
    SenderSock.SetPacketSize(100);

    //Send Data
    std::wstring Data = L"Hello Bello!";
    SenderSock.Send(Data);
}
catch(std::exception& e)
{
    std::wcout << e.what() << std::endl;
}...

The Connect-Function

    int Inc::CSocketTCP::Connect(const std::wstring& IP, const std::wstring& Port)
{
    //NOTE: assert that the socket is valid
    assert(m_Socket != INVALID_SOCKET);

    //for debuggin: convert WStringToString here
    std::string strIP = WStringToString(IP), strPort = WStringToString(Port);

    Incgetaddrinfo AddyResolver;
    addrinfo hints = {}, *pFinal = nullptr;
    
    hints.ai_family = AF_INET;

    //resolve the ip/port-combination for the connection process
    INT Ret = AddyResolver(strIP.c_str(), strPort.c_str(), &hints, &pFinal);
    if(Ret)
    {
        //error handling: throw an error description
        std::string ErrorString("Resolving Process failed (Connect): ");
        ErrorString.append(Inc::NumberToString<INT>(Ret));
        throw(std::runtime_error(ErrorString.c_str()));
    }


    /*---for debbuging---*/
    sockaddr_in *pP = (sockaddr_in*)(pFinal->ai_addr);
    u_short Porty = ntohs(pP->sin_port);
    char AddBuffer[20] = "";

    InetNtopA(AF_INET, (PVOID)&pP->sin_addr, AddBuffer, 20);
    /*--------------------------------------------------------*/


    if(connect(m_Socket, pFinal->ai_addr, pFinal->ai_addrlen) == SOCKET_ERROR)
    {
        int ErrorCode = WSAGetLastError();
        if((ErrorCode == WSAETIMEDOUT) || (ErrorCode == WSAEHOSTUNREACH) || (ErrorCode == WSAENETUNREACH))
        {
            //Just Unreachable
            return TCP_TARGETUNREACHABLE;
        }

        //real errors now
        std::string ErrorString("Connection Process failed: ");
        ErrorString.append(Inc::NumberToString<int>(ErrorCode));
        throw(std::runtime_error(ErrorString.c_str()));
    }

    return TCP_SUCCESS;
}

Additional Information: -Incgetaddrinfo is a function object encapuslating getaddrinfo... -Noone of the server functions return any error and work as expected when stepping through them using the debugger or when letting them run solely...

I'd be glad to hear your suggestions what the problem might be...

It works when I dont connect to ("localhost","12345"), but to ("",12345)... When look into the address resolution process of getaddrinfo it gives 127.0.0.1 for "localhost" and my real IP for ""

Why doesn't it work with my loopback-IP?


Solution

  • You have the answer in your question:

    ... it gives 127.0.0.1 for "localhost" and my real IP for ""

    This means your server binds to the real IP of the interface instead of INADDR_ANY, i.e. it doesn't listen on the loopback.

    Edit 0:

    You don't really need name resolution when creating listening socket. Just bind() it to INADDR_ANY to listen on all available interfaces (including the loopback).