微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 只能将套接字绑定到127.0.0.1

我现在在C#中使用套接字服务器,但我只能将其绑定到127.0.0.1.
但它必须绑定到我的VPS IP.即使我尝试绑定到我的hamachi IP,它也不起作用.

我用:

ServerSocketSettings Settings = new ServerSocketSettings
{
    MaxConnections = Config.ServerMaxConnections,NumOfSAEaForRec = Config.ServerMaxConnections,Backlog = 30,MaxSimultaneousAcceptOps = 15,BufferSize = 512,Endpoint = new IPEndPoint(IPAddress.Parse("25.168.77.190"),Config.ServerPort)
};

this._serverSocket = new ServerSocket(Settings);

然后我做:

this.ListenSocket = new Socket(this.Settings.Endpoint.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
this.ListenSocket.Bind(this.Settings.Endpoint);
this.ListenSocket.Listen(this.Settings.Backlog);

this.Settings是上面代码的值.
当我运行它时,我得到:

The requested address is not valid in its context

我想知道为什么它不起作用.

解决方法

如果要限制此网段的服务可用性,则应将侦听套接字仅绑定到特定接口.

如果不需要,您可以将其绑定到任何IP地址

this.ListenSocket.Bind(new IPEndPoint(IPAddress.Any));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐