如何为自定义应用程序选择静态端口号?
我们有一个自定义应用程序,需要在其自己的端口号上处理请求。我们真的不在乎这个数字是多少,尽管我们在决定后会坚持使用该端口。如何选择最不可能与用户系统上运行的其他应用程序或服务发生冲突的数字?
我们应该遵循什么规则或标准吗?
澄清:一旦我们选择了一个端口,我们就需要坚持下去。不能使用动态的。我们正在构建一个自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于静态应用程序,请考虑检查
/etc/services
以查找不会与您正在使用的其他任何内容发生冲突并且在其他地方不常用的端口。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.如果您无法预测应用程序将运行的确切环境类型,请不要为此烦恼。选择 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.
如果您不关心端口号,也不介意它每次运行程序时都会发生变化,那么在侦听该端口之前就不要绑定该端口(或者如果您想绑定端口,则与端口 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 viafork
, etc.如果您希望为您的应用程序提供唯一的端口号,则需要向 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.