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

时间:2021-11-04 01:04:16

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服务器,我们必须告诉客户它正在运行什么端口。

3 个解决方案

#1


8  

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(代理如鱿鱼),1666(perforce)等。您可以在这里查看已知端口的完整列表,或者查看/ etc / services 。

#2


12  

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.

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

#3


6  

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 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.

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

#1


8  

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(代理如鱿鱼),1666(perforce)等。您可以在这里查看已知端口的完整列表,或者查看/ etc / services 。

#2


12  

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.

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

#3


6  

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 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.

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