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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7275430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:59:46  来源:igfitidea点击:

How to select a static port number for a custom app?

linuxport

提问by ccleve

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.

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

采纳答案by jweyrich

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.

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

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.

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

回答by Michael Berkowski

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

对于静态应用程序,请考虑检查/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

回答by Chris Jester-Young

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.

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

After you begin listening, use getsocknameto 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.

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

回答by Lars

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 Registryfor the IETF. /etc/servicesand other secondary records are populated from the IANA registry.

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

Please do notsimply 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.

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