gpt4 book ai didi

c# - 本地 IPEndPoint 创建的端口选择。端口号重要吗?

转载 作者:行者123 更新时间:2023-11-30 20:52:55 24 4
gpt4 key购买 nike

这是我第一次使用 IPEndPoint,请原谅我缺乏经验。

如果我像这样创建一个 IPEndPoint:

IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.25"), 0);

是否有可能端口 0 已经在使用中,从而产生异常?这里是否有任何类型的约定用于使用哪个本地端口号?我应该只使用随机生成的小于 65,536 的数字吗?是否有最佳或最安全的方法来选择端口,这是否重要?

此特定实例中的用例仅供我了解更多信息。我现在正在摆弄发送 UDP 广播 WOL 数据包,但稍后我希望进入更复杂的代码。我只是想确保我了解最佳实践。

最佳答案

根据 IANA

Port numbers are assigned in various ways, based on three ranges: System Ports (0-1023), User Ports (1024-49151), and the Dynamic and/or Private Ports (49152-65535); the difference uses of these ranges is described in [RFC6335]. System Ports are assigned by IETF process for standards-track protocols, as per [RFC6335]. User Ports are assigned by IANA using the "IETF Review" process, the "IESG Approval" process, or the "Expert Review" process, as per [RFC6335]. Dynamic Ports are not assigned.

同样来自 UdpClient(port) ctor MSDN 文档:

If you pass 0 to the constructor, the underlying service provider will assign a port number.

关于c# - 本地 IPEndPoint 创建的端口选择。端口号重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20434627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com