Skip to content

Conversation

Tornhoof
Copy link
Contributor

@Tornhoof Tornhoof commented Jun 10, 2020

The max port number 5000 is not correct on .NET Core, example code:

        static void Main(string[] args)
        {
            var tcpListener = new TcpListener(IPAddress.Loopback, 0);
            tcpListener.Start();
            var port  = (tcpListener.LocalEndpoint as IPEndPoint).Port;
            Console.WriteLine(port);
            tcpListener.Stop();
            Console.ReadLine();
        }

Output is 62106 (or similar)

Summary

The max port number 5000 is not correct on .NET Core, changed to 65535.

Fixes #Issue_Number (if available)

The max port number 5000 is not correct on .NET Core, example code:
```csharp
        static void Main(string[] args)
        {
            var tcpListener = new TcpListener(IPAddress.Loopback, 0);
            tcpListener.Start();
            var port  = (tcpListener.LocalEndpoint as IPEndPoint).Port;
            Console.WriteLine(port);
            tcpListener.Stop();
            Console.ReadLine();
        }
```
Output is 62106.
@dotnet-bot dotnet-bot added this to the June 2020 milestone Jun 10, 2020
@gewarren gewarren requested a review from a team June 22, 2020 21:06
Copy link
Member

@wfurt wfurt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I'm not sure if we even need to be specific about the range. It is up to underlying OS to decide if 0 is provided. (and 65535 is simply max value for 16bit number)

## Remarks
This constructor allows you to specify the local IP address and port number on which to listen for incoming connection attempts. Before calling this constructor you must first create an <xref:System.Net.IPAddress> using the desired local address. Pass this <xref:System.Net.IPAddress> to the constructor as the `localaddr` parameter. If you do not care which local address is assigned, specify <xref:System.Net.IPAddress.Any?displayProperty=nameWithType> for the `localaddr` parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces. If you do not care which local port is used, you can specify 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 5000. If you use this approach, you can discover what local network address and port number has been assigned by using the <xref:System.Net.Sockets.TcpListener.LocalEndpoint%2A> property.
This constructor allows you to specify the local IP address and port number on which to listen for incoming connection attempts. Before calling this constructor you must first create an <xref:System.Net.IPAddress> using the desired local address. Pass this <xref:System.Net.IPAddress> to the constructor as the `localaddr` parameter. If you do not care which local address is assigned, specify <xref:System.Net.IPAddress.Any?displayProperty=nameWithType> for the `localaddr` parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces. If you do not care which local port is used, you can specify 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 65535. If you use this approach, you can discover what local network address and port number has been assigned by using the <xref:System.Net.Sockets.TcpListener.LocalEndpoint%2A> property.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wfurt So just strip " number between 1024 and 65535"? Seems reasonable.

Suggested change
This constructor allows you to specify the local IP address and port number on which to listen for incoming connection attempts. Before calling this constructor you must first create an <xref:System.Net.IPAddress> using the desired local address. Pass this <xref:System.Net.IPAddress> to the constructor as the `localaddr` parameter. If you do not care which local address is assigned, specify <xref:System.Net.IPAddress.Any?displayProperty=nameWithType> for the `localaddr` parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces. If you do not care which local port is used, you can specify 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 65535. If you use this approach, you can discover what local network address and port number has been assigned by using the <xref:System.Net.Sockets.TcpListener.LocalEndpoint%2A> property.
This constructor allows you to specify the local IP address and port number on which to listen for incoming connection attempts. Before calling this constructor you must first create an <xref:System.Net.IPAddress> using the desired local address. Pass this <xref:System.Net.IPAddress> to the constructor as the `localaddr` parameter. If you do not care which local address is assigned, specify <xref:System.Net.IPAddress.Any?displayProperty=nameWithType> for the `localaddr` parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces. If you do not care which local port is used, you can specify 0 for the port number. In this case, the service provider will assign an available port. If you use this approach, you can discover what local network address and port number has been assigned by using the <xref:System.Net.Sockets.TcpListener.LocalEndpoint%2A> property.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. but I'm fine will leaving the numbers in as well. Even if I don't really see how that would be interesting to anybody. (assuming people understand port is uint16)

Copy link
Contributor Author

@Tornhoof Tornhoof Jun 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPEndPoint.Port (the one from LocalEndPoint) is integer, so they might not know it's ushort.
One could argue that 1024/65535 is an implementation detail and simply refer to IPEndPoint.MinPort (const value of 0) and IPEndPoint.MaxPort (const value of 65535).
e.g.
the service provider will assign an available port number between xref:System.Net.IPEndPoint.MinPort and xref:System.Net.IPEndPoint.MaxPort.
The "1024" information is lost now, but the values refer to the proper min/max of the relevant type.

@gewarren
Copy link
Contributor

gewarren commented Aug 3, 2020

@Tornhoof Thanks, I'll merge this now since we seem to be okay with leaving the min/max numbers in there.

@gewarren gewarren merged commit e61b32d into dotnet:master Aug 3, 2020
@Tornhoof Tornhoof deleted the patch-1 branch August 3, 2020 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants