Reserve a TCP port in Windows

后端 未结 5 1716
小鲜肉
小鲜肉 2021-02-01 23:14

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 po

相关标签:
5条回答
  • 2021-02-01 23:33

    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.

    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.

    0 讨论(0)
  • 2021-02-01 23:33

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

    0 讨论(0)
  • 2021-02-01 23:44

    As mentioned by @vahapt you can modify the dynamic port range using 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.

    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

    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)

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

    0 讨论(0)
  • 2021-02-01 23:46

    Using netsh command 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.

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

    0 讨论(0)
  • 2021-02-01 23:47

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

    You can edit the 'ReservedPorts' Registry Setting in

    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'

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

    0 讨论(0)
提交回复
热议问题