Neel
Neel

Reputation: 21

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 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?

Upvotes: 0

Views: 728

Answers (2)

Dubs
Dubs

Reputation: 124

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.

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151654

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

Upvotes: 2

Related Questions