在 Windows 中保留 TCP 端口 [英] Reserve a TCP port in Windows

查看:25
本文介绍了在 Windows 中保留 TCP 端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保留一个 TCP 端口,以便稍后由服务绑定,以便 Windows 在分配随机端口号时不会无意中使用相同的端口号.我知道这可以通过注册表和重新启动来实现,但我想避免这种笨拙的解决方案.

I'd like to reserve a TCP port, to be bound by a service later, so that Windows doesn't inadvertently use the same number when assigning random port numbers. I know this is possible via the registry and a reboot, but I would like to avoid such a heavy-handed solution.

进程如何在不实际绑定/侦听端口的情况下保留端口,然后根据请求安全地(即避免竞争条件)将其移交给另一个进程?

How can a process reserve a port without actually binding/listening to it, and then safely (i.e., avoiding race-conditions) hand it over to another process on request?

端口号无需提前确定.第一个进程可以获取一个随机端口号,并传递给请求进程.

The port number needn't be determined in advance. It's OK for the first process to acquire a random port number, and pass it to the requesting process.

我突然想到我的问题表述得有些不好.我真正想要的是将动态端口号的分配与绑定到端口零操作分开.这意味着不仅要避免意外随机分配该端口号,还要防止任何其他进程在此期间绑定到同一地址/端口.或者,换一种说法,我希望一个进程启动绑定到端口零的操作——立即了解将使用的端口号——并让指定的第二个进程在未来某个时间完成绑定操作.

It occurs to me that my question is somewhat poorly stated. What I really want is to separate the allocation of a dynamic port number from the bind-to-port-zero operation. This means not just avoiding accidental random allocation of that port number, but also preventing any other process from binding to the same address/port in the interim. Or, putting it another way, I want one process to start the bind-to-port-zero operation — immediately learning the port number that will be used — and let a nominated second process complete the bind operation sometime in the future.

目前,我能想到的最接近的解决方法是让第一个进程立即绑定到 address/0,并保持绑定直到第二个进程请求它,此时它解除绑定并告诉另一个进程它获取的端口号,然后显式绑定到地址/端口.这有两个问题:1)我宁愿在第二个过程出现之前根本不绑定;2) 有一小段时间间隔,在此期间第三方可能会意外(或故意)篡夺端口.

At the moment, the closest work-around I can think of is for the first process to bind to address/0 immediately, and stay bound until the second process requests it, at which point it unbinds and tells the other process the port number it acquired, which then binds to the address/port explicitly. This has two problems: 1) I'd rather not bind at all until the second process comes along; 2) there's a small time interval during which a third party could accidentally (or deliberately) usurp the port.

你可能会好奇我为什么要做这么奇怪的事情.我一直在玩弄 ZeroMQ,一个主要的限制是 Windows 上没有 ipc:// 传输.让我印象深刻的是,端口映射器进程(类似于 RPC 端点映射器,或 Erlang 的 epmd)只是使用带有动态端口分配的 tcp:// 传输实现变通方案的门票.但是,ZeroMQ 客户端和服务器允许无序连接(即,客户端在服务器绑定之前连接不是错误),所以我试图弄清楚连接客户端如何发现 - 非常高度确定性——在服务器实际绑定到该端口之前,将用于通信的端口.

You may be curious as to why I wish to do something so odd. I've been toying with ZeroMQ, and one major limitation is the absence of the ipc:// transport on Windows. It struck me that a port mapper process (akin to the RPC endpoint mapper, or Erlang's epmd) would be just the ticket to implement a work-around using the tcp:// transport with dynamic port allocations. However, ZeroMQ clients and servers are allowed to connect out of order (i.e., it isn't an error for the client to connect before the server binds), so I am trying to figure out how a connecting client can discover — with a very high degree of certainty — the port that will be used to communicate, before a server actually binds to that port.

推荐答案

正如@vahapt 所说,您可以使用 netsh 修改动态端口范围.

As mentioned by @vahapt you can modify the dynamic port range using netsh.

但是,更好的解决方案可能是使用 netsh 来保留应用程序所需的端口,而不管动态端口的默认范围.

However, a better solution may be to use netsh to reserve the ports required by your application, leaving alone the default range of dynamic ports.

这样做:

  1. 在 Server 2008/2008 R2 上,安装此 Microsoft 修补程序.这在 Server 2012 或更高版本上不是必需的.
  2. 停止使用要保留的端口的任何进程.如果进程正在使用包含在要保留的端口范围内的端口,NETSH 将返回以下错误并且保留将失败:

  1. On Server 2008/2008 R2, install this Microsoft hotfix. This is not required on Server 2012 or later.
  2. Stop any processes using the ports to be reserved. If a process is using a port included in the range of ports to be reserved, NETSH will return the following error and the reservation will fail:

该进程无法访问该文件,因为它正被另一个进程使用.

The process cannot access the file because it is being used by another process.

  • 使用以下 NETSH 命令保留端口:

  • Use the following NETSH command to reserve the ports:

    netsh int <ipv4|ipv6>添加排除端口范围 [protocol=]tcp|udp [startport=]<integer>[numberofports=]<整数>[[store=]active|persistent]

    例如,要为 UDPv6 保留端口 55368-55372,请使用以下命令:

    For example, to reserve ports 55368-55372 for UDPv6, use the command:

    netsh int ipv6 add excludeportrange protocol=udp startport=55368 numberofports=5

    注意事项:

    • 默认情况下,端口保留在重启后保持不变
    • 可以为协议的第 4 版或第 6 版保留端口,但不能同时为两者保留(即您不能为 TCPv4 和 TCPv6 保留端口 60000)

    请参阅 https://support.microsoft.com/en-us/kb/929851 了解更多信息,包括如何查看或删除现有的港口预留.

    See https://support.microsoft.com/en-us/kb/929851 for more information, including how to view or delete existing port reservations.

    这篇关于在 Windows 中保留 TCP 端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆