Connect() fails with error 10061 (WSAECONNREFUSED) / datagram sent via sendto() lost / loopback address
A server application (TCP) using socket functions is waiting on NIC address to receive connection from the client.
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("10.171.xx.xx");
service.sin_port = htons(27015);
Now a client application on same machine uses connect() to connect to the server application get error 10061 (WSAECONNREFUSED) when the IP Address used is loopback address (127.0.0.1).
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( DEFAULT_PORT);
The same way, when an application using UDP protocol uses recvfrom() on NIC’s address, like below:
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr("10.171.xx.xx");
And the UDP sender running on same machine is sending the datagram on loopback address (127.0.0.1), the packet does not get received by the receiver, sent on same port.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
This is happening on Windows Vista onwards Operating System, while it used to be working on earlier versions of OS. This is design change on Windows Vista onwards, Strong host model vs WeAk host model. Windows Vista onwards, we have strong host model on by default on all interfaces.
This can also be a problem if you connect() to a real IP address on interface A and the packets are trying to go out another interface, Interface B. This can occur when the default route points to Interface B.
Please check for more details- https://technet.microsoft.com/en-us/magazine/2007.09.cableguy.aspx
Attached is the minimal code sample to demonstrate the behavior explained above. You will need to change the IP Addresses mentioned to your machine’s adapter IP address.
More references- RFC 3484
Content developed by: Nitin Dhawan
Content reviewed by: Jeff Lambert