tags:

views:

46

answers:

2

What is the max value of the port i can assing on my socket when i do a bind

example:

int port = 0; //How far can i go?
Socket m_mainSocket;
m_mainSocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1), port))

Thanx

+2  A: 

IP port numbers are 16-bit unsigned integers. Therefore 65,535 is the maximum port number you can assign.

The documentation for the IPEndPoint constructor states that an ArgumentOutOfRangeException will be raised if the port is greater than MaxPort. The documentation for MaxPort states that 'the MaxPort value is set to 0x0000FFFF' (65,535).

Phil Ross
+1 for explaining why..
Dusty Roberts
Note that a lot of ports - especially in the higher number range - are reserved for use as [ephemeral ports](http://en.wikipedia.org/wiki/Ephemeral_port) and *should* not be bound to.
Stephen Cleary
A: 

The IPEndpoint class has a MaxPort and a MinPort field - according to the documentation:

The MaxPort value is set to 0x0000FFFF.

Lee