Question

Now a socket in server side binds 192.168.1.69:9000,and then start to listen. Client connects the server using 127.0.0.1:9000. But fail. However,it works when client connect the server using 192.168.1.69:9000.
Client and server are both running on the same commputer.
My question is: it should be successful When client using loopback address connect the server, but fail.Why?

Server Code:
this.pro_ListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this.pro_ListenSocket.ReceiveBufferSize = this.pro_BufferSize;
this.pro_ListenSocket.SendBufferSize = this.pro_BufferSize;
try
{
     this.pro_ListenSocket.Bind(new IPEndPoint(this.pro_ServerIP, this.pro_Port));
}
catch (SocketException socketError)
{
     return false;
}
catch (Exception)
{
     return false;
}
try
{
     this.pro_OnRunning = true;
     this.pro_ListenSocket.Listen(500);
     this.StartToAcceptClient(this.pro_ListenSocket);
}
catch (Exception ex)
{
    return false;
}
Was it helpful?

Solution

Loopback is represented as a network adapter, just like any other. You have set your server to only listen for connections on the adapter at 192.168.1.69. If you want your server to listen on additional adapters, the easiest approach is to make it available on all available adapters by specifying the address IPAddress.Any (0.0.0.0).

this.pro_ListenSocket.Bind(new IPEndPoint(IPAddress.Any, this.pro_Port));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top