gpt4 book ai didi

c# - 只能绑定(bind)socket到127.0.0.1

转载 作者:行者123 更新时间:2023-11-30 12:46:25 25 4
gpt4 key购买 nike

我现在在 C# 中使用套接字服务器,但我只能将它绑定(bind)到 127.0.0.1。但它必须绑定(bind)到我的 VPS IP。即使我尝试绑定(bind)到我的 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

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

最佳答案

如果您想将服务的可用性限制在该网段,则应仅将监听套接字绑定(bind)到特定接口(interface)。

如果不需要,您可以将它绑定(bind)到任何 ip 地址

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

关于c# - 只能绑定(bind)socket到127.0.0.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20383353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com