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
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.
For ZeromMQ, you can use the zbeacon
module from czmq or C# NetMq to implement service discovery.
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:
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.
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:
See https://support.microsoft.com/en-us/kb/929851 for more information, including how to view or delete existing port reservations.
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.
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