加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:59:24 所属栏目:百科 来源:网络整理
导读:我现在在C#中使用套接字服务器,但我只能将其绑定到127.0.0.1. 但它必须绑定到我的VPS IP.即使我尝试绑定到我的hamachi IP,它也不起作用. 我用: ServerSocketSettings Settings = new ServerSocketSettings{ MaxConnections = Config.ServerMaxConnections,N
我现在在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));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读