客户端无法访问C#中绑定环回地址的服务器

时间:2013-10-23 15:49:46

标签: sockets loopback-address

现在服务器端的套接字绑定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;
}

1 个答案:

答案 0 :(得分:1)

Loopback表示为网络适配器,就像其他任何一样。您已将服务器设置为仅侦听适配器上的连接192.168.1.69。如果您希望服务器监听其他适配器,最简单的方法是通过指定地址IPAddress.Any(0.0.0.0)使其在所有可用适配器上可用。

this.pro_ListenSocket.Bind(new IPEndPoint(IPAddress.Any, this.pro_Port));
相关问题