现在,服务器端的套接字将192.168.1.69:9000绑定,然后开始侦听。客户端使用127.0.0.1:9000连接服务器。但是失败了。但是,它在客户端使用192.168.1.69:9000连接服务器时起作用。
客户端和服务器都在同一台计算机上运行。
我的问题是:应该成功当使用环回地址的客户端连接服务器但失败时,为什么?
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;
}
像其他任何环回一样,环回表示为网络适配器。您已将服务器设置为仅侦听192.168.1.69适配器上的连接。如果希望服务器在其他适配器上进行侦听,最简单的方法是通过指定地址IPAddress.Any
(0.0.0.0)使它在所有可用适配器上都可用。
this.pro_ListenSocket.Bind(new IPEndPoint(IPAddress.Any, this.pro_Port));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句