How to select a static port number for a custom ap

2019-04-18 11:49发布

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.

标签: linux port
4条回答
等我变得足够好
2楼-- · 2019-04-18 12:15

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.

查看更多
beautiful°
3楼-- · 2019-04-18 12:17

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
查看更多
太酷不给撩
4楼-- · 2019-04-18 12:23

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.

查看更多
Deceive 欺骗
5楼-- · 2019-04-18 12:24

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.

查看更多
登录 后发表回答