Finding Free Ports, Part 2

Yesterday I talked about the algorithm the TCP transport uses to reserve a unique port when listening on both IPv4 and IPv6 addreses is enabled for a service. Why are the random port numbers drawn from the range 49152-65535?

Because that’s the range the operating system uses for dynamic port allocations. Sometimes.

The IANA divides the range of ports into three parts: well-known ports which are less than 1024, registered ports which are 1024 through 49151, and dynamic ports which are greater than 49151. Well-known and registered ports are different kinds of classifications for ports that have an assigned application use. Dynamic ports are intended for transient use by applications, which is what we want in this case.

Up through Windows Server 2003 though, the dynamic port range was 1025 to 5000 (you could use a registry setting to increase the maximum port number). Starting in Windows Vista the dynamic port range was changed to match the IANA recommendation (you can use the netsh command to change both the minimum and maximum port number).

WCF runs on versions of Windows back to Windows XP so there was a question of what dynamic port range to use. What we ultimately decided was to use a fixed range rather than duplicate the operating system settings for controlling the dynamic port range and to use the standardized port range on both new and old systems.