今天在看源代码的时候,发现有个端口0,不太明白其含义
class robotkillerserver(socketserver.tcpserver):
allow_reuse_address = true
def init(self, debugger):
socketserver.tcpserver.init(self, ("",0), robotkillerhandler)
self.debugger = debugger
1
2
3
4
5
从网上找了一篇英文看了看:
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时有一些细微的差别。