tcp dynamic port number

在服務器listen port時,port選擇需要注意衝突問題。以windows os來說,

Microsoft has increased the dynamic client port range for outgoing connections in Windows Vista and Windows Server 2008. The new default start port is 49152, and the new default end port is 65535. This is a change from the configuration of earlier versions of Windows that used a default port range of 1025 through 5000.

早期的dynamic port range選在1025-5000,目前windows按照IANA的建議選在49152 – 65535。 (client program port)

因此server port選在49152 – 65535則會有衝突的可能,若client的連線剛好占用到此port,這樣一來就無法bind成功。看起來就是server偶爾會listen失敗,通常遇到的時候看錯誤代碼應可推敲出問題,但這類的錯誤出現是機率性的,在測試時不一定會發生,因此在一開始設計時必須要理解有這樣的問題可能。

Linux可參考 /proc/sys/net/ipv4/ip_local_port_range (32768 60999)

參考:
https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml
https://support.microsoft.com/en-us/help/929851/the-default-dynamic-port-range-for-tcp-ip-has-changed-in-windows-vista

This entry was posted in System Administration. Bookmark the permalink.

Leave a Reply