您如何决定使用哪个端口?

发布于 2024-08-20 10:10:19 字数 408 浏览 4 评论 0原文

这有点主观,因为可以说没有规则。每次创建服务器时,我都会想,“最好使用的端口是什么?”我猜答案是“任何,只要用户可以更改它”。那么,其他人如何决定如何选择默认端口呢?就个人而言,如果与 HTTP 相关,我喜欢使用 8000 之类的东西,并且我注意到这是一个非常常见的趋势。但如果 8000 已经在使用怎么办?用8001吗?这似乎有点特别,我想确实如此。

显然我不是第一个问这个问题的人。 IANA 维护着一个端口号列表...这让我看到了未分配的范围 (48620 -49150)。我想我们确实应该使用这些,但为什么没有更多的程序员这样做呢?您如何决定使用哪个;如果每个人都从#1 开始,那么我们都会使用 48620。

This is a little subjective, as there are no rules so to speak. Every time I create a server, I think to myself, "What is the best port to use?" I guess an answer is "Any, as long as the user can change it." So, how does everyone else decide how to choose the default port? Personally, I like to use something like 8000-something if it's HTTP related, and I've noticed this is a pretty common trend. But what if 8000 is already in use? Use 8001? It seems a little ad-hoc, and I suppose it is.

Clearly I'm not the first to have asked this question; IANA maintain a port numbers list... Which leads me on to the unassigned range (48620-49150). I guess we should really be using these, but why don't more programmers do so? How do you decide which to use; if everyone started at #1, then we'd all be using 48620.

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

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

发布评论

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

评论(8

忘羡 2024-08-27 10:10:19

我认为您已经尽可能地回答了您的问题;除了您所说的之外,实际上并没有您可以遵循的严格规则。但一般来说:

  • 查看 IANA 列表并选择一个未使用的端口。
  • 选择一个容易记住的端口号。
  • 不要修复代码中的端口号。其他一些产品可能选择了与您相同的端口,并且您永远不知道何时必须在服务器上共存,因此将端口号放在配置文件中的某个位置,以便在必要时可以更改它而无需重新编译。更改端口号的功能也有助于通过防火墙,而无需重新配置它们。 (如果配置文件不存在,您始终可以默认为您选择的值。)
  • 有一个论点是您不想选择太高的值,因为您可能会与 临时端口。您不太可能会受到这种情况的影响,但当它发生时,这是一个很难调试的问题。

(如果您想知道如何选择令人难忘的端口号,我曾经与一个人一起工作,他根据同事的电话分机记住了端口号。)

I think you've pretty much answered your question as much as is possible; there isn't really a strict rule you can follow here beyond what you've said. But generally:

  • Look at the IANA list and pick a port that's not in use.
  • Pick a port number that is easy to remember.
  • Don't fix the port number in your code. Some other product may have picked the same port as you and you never know when you'll have to co-exist on a server, so put the port number in a configuration file somewhere so it can be changed without a recompile if necessary. The ability to change port number can also be helpful for getting through firewalls without having to get them reconfigured. (You can always default to your chosen value if the configuration file doesn't exist.)
  • There is an argument that you don't want to pick something too high as you may conflict with the range used for ephemeral ports. It's not that likely that you'll get hit by this, but it's a hard problem to debug when it happens.

(And if you want a tip for picking memorable port numbers, I once worked with someone who remembered port numbers based around the telephone extensions of his co-workers.)

梦断已成空 2024-08-27 10:10:19

一些易于记忆且适当的未分配端口(根据 IANA):

27182 (e)

31415 (pi)

60221 (avagadro's)

Some easy to remember and appropriately nerdy unassigned (per IANA) ports:

27182 (e)

31415 (pi)

60221 (avagadro's)

风情万种。 2024-08-27 10:10:19

在测试期间...始终端口 #666 ;)

During testing... always port #666 ;)

叶落知秋 2024-08-27 10:10:19

怎么样:

defaultPort = (new Random()).Next(48620, 49150);

How about:

defaultPort = (new Random()).Next(48620, 49150);
梦初启 2024-08-27 10:10:19

你回答了你自己的问题吗?选择任何未分配的端口并允许用户更改它。

You answered your own question? Pick any unassigned port and allow the user to change it.

吲‖鸣 2024-08-27 10:10:19

我更喜欢这种方式:(python代码如下)

#!/usr/bin/env python3
import random as R
r = R.SystemRandom()
print([r.randrange(1024, 65535) for x in range(4)])

然后我选择我最喜欢的数字。
或者,如果您对可接受的数字有更严格的限制,则可以更改范围。

I prefer this way: (python code following)

#!/usr/bin/env python3
import random as R
r = R.SystemRandom()
print([r.randrange(1024, 65535) for x in range(4)])

And then I pick the number which I like the most.
Or course, change the range if you have some stricter limits of what are acceptable numbers.

请叫√我孤独 2024-08-27 10:10:19

经过快速谷歌搜索以确保其清晰后,我通常只选择一些对个人有意义的数字。

After a quick Google search to make sure it's clear, I generally just choose a number of personal significance.

猫腻 2024-08-27 10:10:19

我建议永远不要使用像 5 位数字这样的大端口,因为它可能会影响其他操作系统进程并分配临时端口。由于其限制,您将开始收到“已在使用错误”。

I'd suggest never use a port that is a big number like 5 digits, as it might hit some other operation system processes and assigns the Ephemeral ports. You would start to get 'Already in use errors' due to its limitations.

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