Reserve an Ephemeral UDP Port for Your App

In a Windows server, when using an ephemeral UDP Port for a new UDPClient connection (such as MyUdpClient = new UdpClient(49156);), you may encounter that the specific port you want to use happens to be eventually occupied by another process.

A means to check out if the port is occupied is to use the next command: netstat -abon | find "UDP" | more . In my case I found out that the port was being used by the DNS Server.

One solution is to firstly stop the DNS Server (the service occupying the ports for my application). Secondly, restart my application, so that the ephemeral ports get occuppied. And, finally, start the DNS Server again.

Another solution is to reserve the specific ports. A means to reserve that specific port is to register it in the key ReservedPorts (reference) within HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters of the windows registry. If ReservedPorts does not exist yet, you can create a new “Multi-string Value”. A more detailed step-by-step of this problem and solution can be found in this link: Fun with UDP conflicts and DNS port randomization. Further reference: https://support.microsoft.com/en-us/kb/956189

This fix works only for Windows Server 2000 and 2003. For WS 2008, you cannot exclude ports by using the ReservedPorts registry key. Here you find a hotfix to the problem.