在 Windows 中保留 TCP 端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5254330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:10:00  来源:igfitidea点击:

Reserve a TCP port in Windows

windowsnetwork-programmingzeromq

提问by Marcelo Cantos

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.

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

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.

端口号不需要预先确定。第一个进程获取一个随机端口号,并将其传递给请求进程是可以的。

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

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

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.

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

Background

背景

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.

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

回答by I say Reinstate Monica

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

正如@vahapt 所提到的,您可以使用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.

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

To do so:

这样做:

  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.

  3. Use the following NETSH command to reserve the ports:

    netsh int <ipv4|ipv6> Add excludedportrange [protocol=]tcp|udp [startport=]<integer> [numberofports=]<integer> [[store=]active|persistent]

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

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

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

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

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

    netsh int <ipv4|ipv6> Add excludedportrange [protocol=]tcp|udp [startport=]<integer> [numberofports=]<integer> [[store=]active|persistent]

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

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

Notes:

笔记:

  • By default port reservations are persistent across reboots
  • Ports may be reserved for either version 4 or 6 of a protocol, but not both (i.e. you cannot reserve port 60000 for both TCPv4 and TCPv6)
  • 默认情况下,端口保留在重新启动后是持久的
  • 可以为协议的第 4 版或第 6 版保留端口,但不能同时为两者保留(即您不能为 TCPv4 和 TCPv6 保留端口 60000)

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

有关详细信息,包括如何查看或删除现有端口保留,请参阅https://support.microsoft.com/en-us/kb/929851

回答by vahapt

Using netshcommand might help you. You can change the dynamic port range used by Windows.
It is like the registry modification that you indicated, but it is effective immediately.

使用netsh命令可能会对您有所帮助。您可以更改 Windows 使用的动态端口范围。
就像你说的修改注册表一样,但是是立即生效的。

see: http://support.microsoft.com/kb/929851for details about netsh command.

有关 netsh 命令的详细信息,请参见:http: //support.microsoft.com/kb/929851

回答by edhurtig

Edit: This only applies to pre-Windows Server 2008 (Microsoft Support KB)

编辑:这仅适用于 Windows Server 2008 之前的版本(Microsoft 支持知识库

You can edit the 'ReservedPorts' Registry Setting in

您可以编辑“ReservedPorts”注册表设置

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters

To reserve a range of ports follow the format '4000-4010' or 'xxxx-yyyy' however to reserve a single port you have to use the format of '4000-4000' or 'xxxx-xxxx'

要保留一系列端口,请遵循“4000-4010”或“xxxx-yyyy”格式,但要保留单个端口,您必须使用“4000-4000”或“xxxx-xxxx”格式

http://support.microsoft.com/kb/812873

http://support.microsoft.com/kb/812873

回答by Marcelo Cantos

I've come up with a possible solution, so I thought I may as well document it here as an answer.

我想出了一个可能的解决方案,所以我想我也可以在这里记录它作为答案。

A process can pass a socket over to another process via a call to WSADuplicateSocket, so a coordinating process could bind to a dynamic port, and internally associate it with a given IPC name. When a ZMQ server process wanting to "bind" to that name arrives, the coordinating process copies the bound socket to the server process and closes its own copy.

进程可以通过调用 WSADuplicateSocket 将套接字传递给另一个进程,因此协调进程可以绑定到动态端口,并在内部将其与给定的 IPC 名称相关联。当想要“绑定”到该名称的 ZMQ 服务器进程到达时,协调进程将绑定的套接字复制到服务器进程并关闭自己的副本。

This solution doesn't address my preference to avoid calling bind(), but that may not be strictly necessary; I'll have to perform some tests.

这个解决方案没有解决我避免调用 bind() 的偏好,但这可能不是绝对必要的;我将不得不进行一些测试。

回答by dkrikun

For ZeromMQ, you can use the zbeaconmodule from czmq or C# NetMq to implement service discovery.

对于 ZerommQ,您可以使用zbeaconczmq 或 C# NetMq 中的模块来实现服务发现。