端口0的含義
來源 https://blog.csdn.net/u011546806/article/details/44979999
今天在看源代碼的時候,發現有個端口0,不太明白其含義
class RobotKillerServer(SocketServer.TCPServer): allow_reuse_address = True def __init__(self, debugger): SocketServer.TCPServer.__init__(self, ("",0), RobotKillerHandler) self.debugger = debugger
Purpose:
Port 0 is officially a reserved port in TCP/IP networking, meaning that it should not be used for any TCP or UDP network communications. However, port 0 sometimes takes on a special meaning in network programming, particularly Unix socket programming. In that environment, port 0 is a programming technique for specifying system-allocated (dynamic) ports.
端口號 0 是一個預留的端口號,代表的意思就是它在TCP或者UDP網絡傳輸中應該不會被用到。但是在網絡編程中,尤其是在unix socket編程當中,它有一些特殊的含義。在unix socket編程當中,端口號 0 是一種由系統指定動態生成的端口。
Description:
Configuring a new socket connection requires assigning a TCP or UDP port number. Instead of hard-coding a particular port number, or writing code that searches for an available port on the local system, network programmers can instead specify port 0 as a connection parameter. That triggers the operating system to automatically search for and return the next available port in the dynamic port number range.Unix, Windows and other operating systems vary slightly in their handling of port 0.
當建立新的TCP和UDP socket連接時,需要給它們指定端口號。 為了避免這種寫死端口號的做法或者說為了從本地系統中找到可用端口。網絡編程員們可以以端口號0來作為連接參數。這樣的話操作系統就會從動態端口號范圍內搜索接下來可以使用的端口號。windows系統和其他操作系統在處理端口號0時有一些細微的差別。
============== End