套接字 - IPEndpoint端口(最大值)

时间:2010-08-05 09:10:30

标签: c# sockets tcp

当我执行绑定时,我可以在套接字上分配的端口的最大值是什么?

示例:

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

3 个答案:

答案 0 :(得分:11)

IP端口号是16位无符号整数。因此,65,535是您可以分配的最大端口号。

IPEndPoint constructor的文档指出,如果端口大于MaxPort,则会引发ArgumentOutOfRangeExceptionMaxPort的文档说明'MaxPort值设置为0x0000FFFF'(65,535)。

答案 1 :(得分:0)

根据documentation,IPEndpoint类有一个MaxPort和一个MinPort字段:

  

MaxPort值设置为   0x0000FFFF。

答案 2 :(得分:0)

To programmatically access the maximum allowed value, you can use IPEndPoint.MaxPort and IPEndPoint.MinPort for the minimum value. These are useful for checking that a port that the user input is within the proper bounds before actually trying to use it in a connection.