TCP 调试错误 C#

发布于 2024-12-03 00:24:45 字数 526 浏览 3 评论 0原文

public Server([Optional, DefaultParameterValue(0x6c1)] int port, [Optional, DefaultParameterValue("127.0.0.1")] string ip)
{
    this.IP = IPAddress.Parse(ip);
    this.Port = port;
    this.listenerConnection = new TcpListener(this.IP, this.Port);
    this.listenerConnection.Start();
    this.listenerThread = new Thread(new ThreadStart(this.listen));
    this.listenerThread.Start();
}

是我的代码,它运行良好,但是当我调试它时,我收到消息:

指定的参数超出了有效值的范围。参数名称:端口

有人可以帮忙吗?

public Server([Optional, DefaultParameterValue(0x6c1)] int port, [Optional, DefaultParameterValue("127.0.0.1")] string ip)
{
    this.IP = IPAddress.Parse(ip);
    this.Port = port;
    this.listenerConnection = new TcpListener(this.IP, this.Port);
    this.listenerConnection.Start();
    this.listenerThread = new Thread(new ThreadStart(this.listen));
    this.listenerThread.Start();
}

is the code I have, it runs fine but when I debug it, I get the message:

Specified argument was out of the range of valid values. Parameter name: port

Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗化ㄋ丶相逢 2024-12-10 00:24:45

好吧,那么 port 超出了有效值的范围,该范围在 IPEndPoint.MinPortIPEndPoint.MaxPort

Well, then port is out of the range of valid values, which is between IPEndPoint.MinPort and IPEndPoint.MaxPort.

纸伞微斜 2024-12-10 00:24:45

您是否尝试过使用您机器的 IP 地址?您可以使用以下代码来获取正在运行应用程序的计算机的 IP 地址:

IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
IPAddress localIpAddress = null;

forach(IPAddress address in host.AddressList)
{
    if(address.AddressFamily == AddressFamily.InterNetwork)
    {
          localIpAddress = address;
    }
}

TcpListener listener = new TcpListener(localIpAddress, port);
listener.Start();

此外,您可能需要考虑使用默认端口 > 5000。因为 0 到 5000 之间有许多端口已被服务保留或已使用。

Have you tried using the IPAddress of your machine? You can use the following code to obtain the IPAddress of the machine you are running the application on:

IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
IPAddress localIpAddress = null;

forach(IPAddress address in host.AddressList)
{
    if(address.AddressFamily == AddressFamily.InterNetwork)
    {
          localIpAddress = address;
    }
}

TcpListener listener = new TcpListener(localIpAddress, port);
listener.Start();

Additionally, you may want to consider using a default port > 5000. As there are many ports between 0 and 5000 that are reserved or already use by services.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文