开发者

TCP Debug Error C#

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 Thread开发者_StackOverflow中文版Start(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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜