如何为自定义应用程序选择静态端口号?

发布于 2024-12-02 07:32:29 字数 225 浏览 0 评论 0原文

我们有一个自定义应用程序,需要在其自己的端口号上处理请求。我们真的不在乎这个数字是多少,尽管我们在决定后会坚持使用该端口。如何选择最不可能与用户系统上运行的其他应用程序或服务发生冲突的数字?

我们应该遵循什么规则或标准吗?

澄清:一旦我们选择了一个端口,我们就需要坚持下去。不能使用动态的。我们正在构建一个自定义 SFTP 服务器,我们必须告诉客户它在哪个端口上运行。

We've got a custom application that needs to serve requests on it's own port number. We really don't care what the number is, although we'll stick to that port after we decide. How do I select a number which is least likely to conflict with other applications or services that are running on the user's system?

Are there any rules or standards we should follow?

A clarification: once we pick a port, we need to stick with it. Can't use a dynamic one. We're building a custom SFTP server and we'll have to tell our customers what port it's running on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吲‖鸣 2024-12-09 07:32:29

对于静态应用程序,请考虑检查 /etc/services 以查找不会与您正在使用的其他任何内容发生冲突并且在其他地方不常用的端口。

$ tail /etc/services
nimspooler      48001/udp                       # Nimbus Spooler
nimhub          48002/tcp                       # Nimbus Hub
nimhub          48002/udp                       # Nimbus Hub
nimgtw          48003/tcp                       # Nimbus Gateway
nimgtw          48003/udp                       # Nimbus Gateway
com-bardac-dw   48556/tcp                       # com-bardac-dw
com-bardac-dw   48556/udp                       # com-bardac-dw
iqobject        48619/tcp                       # iqobject
iqobject        48619/udp                       # iqobject

For a static application, consider checking /etc/services to find a port that will not collide with anything else you are using and isn't in common use elsewhere.

$ tail /etc/services
nimspooler      48001/udp                       # Nimbus Spooler
nimhub          48002/tcp                       # Nimbus Hub
nimhub          48002/udp                       # Nimbus Hub
nimgtw          48003/tcp                       # Nimbus Gateway
nimgtw          48003/udp                       # Nimbus Gateway
com-bardac-dw   48556/tcp                       # com-bardac-dw
com-bardac-dw   48556/udp                       # com-bardac-dw
iqobject        48619/tcp                       # iqobject
iqobject        48619/udp                       # iqobject
久而酒知 2024-12-09 07:32:29

如果您无法预测应用程序将运行的确切环境类型,请不要为此烦恼。选择 1024 以上的任何数字,并使其可配置,以便用户可以在与其他服务/应用程序发生冲突时更改它。

当然,您仍然可以避免使用非常常见的端口,例如 8080(替代 HTTP)或 3128(squid 等代理)、1666(perforce)等。您可以查看已知端口的完整列表 此处,或查看 /etc/services。

If you can't predict the exact kind of environment your application is going to run, just don't bother with this. Pick any number over 1024 and also make it configurable so the user can change it in case of conflict with another service/application.

Of course you can still avoid very common ports like 8080 (alternative HTTP) or 3128 (proxies like squid), 1666 (perforce), etc. You can check a comprehensive list of known ports here, or take a look at /etc/services.

青朷 2024-12-09 07:32:29

如果您不关心端口号,也不介意它每次运行程序时都会发生变化,那么在侦听该端口之前就不要绑定该端口(或者如果您想绑定端口,则与端口 0 绑定)特定的 IP 地址)。在这两种情况下,您都告诉操作系统为您选择一个空闲端口。

开始侦听后,使用 getsockname 找出选择了哪个端口。您可以将其写入文件,在屏幕上显示,让子级通过 fork 继承它,等等。

If you don't care about the port number, and don't mind that it changes every time your program is run, simply don't bind the port before you listen on it (or bind with port 0, if you want to bind a specific IP address). In both cases, you're telling the OS to pick a free port for you.

After you begin listening, use getsockname to find out which port was picked. You can write it to a file, display on it on the screen, have a child inherit it via fork, etc.

﹉夏雨初晴づ 2024-12-09 07:32:29

如果您希望为您的应用程序提供唯一的端口号,则需要向 IANA 请求分配,维护服务名称和传输协议IETF 的端口号注册表/etc/services 和其他辅助记录是从 IANA 注册表填充的。

不要简单地选择一个号码并发送您的申请(如另一个答案中所述),因为迟早,IANA 会将您占用的端口分配给传入请求,这可能会导致您的申请和不知情的受让人之间存在冲突。

If you want a unique port number for your application, you need to request an assignment from IANA, who maintain the Service Name and Transport Protocol Port Number Registry for the IETF. /etc/services and other secondary records are populated from the IANA registry.

Please do not simply pick a number and ship your application (as mentioned in another answer), because sooner or later, IANA will assign the port you're squatting on to an incoming request, which can lead to conflicts for your application and the unaware assignee.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文