【问题标题】:Linux: what happens when binding on port 0Linux:在端口 0 上绑定时会发生什么
【发布时间】:2019-05-01 14:21:05
【问题描述】:

当我尝试在端口 0 (linux 5.0) 上 bind(2) 时会发生什么?

使用 BSD nc:nc -l 0 有效,并且跟踪它给了我:

bind(3, {sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
listen(3, 1)                            = 0
accept4(3, 

但正在尝试连接:

$ nc 127.1 0
nc: port number too small: 0

这里发生了什么?

【问题讨论】:

    标签: linux unix networking


    【解决方案1】:

    在尝试绑定端口 0 时,实际上选择了一个随机端口。

    试试这个:

    nc -l 0
    ss -4 -t -l # List active listening TCP sockets on IPv4
     # ctrl+c nc to close its socket
    ss -4 -t -l # And check the difference
    

    我的给:

    State   Recv-Q  Send-Q    Local Address:Port       Peer Address:Port  
    LISTEN  0       0             127.0.0.1:58505           0.0.0.0:*    
    LISTEN  0       0             127.0.0.1:46253           0.0.0.0:*    
    LISTEN  0       0         192.168.122.1:domain          0.0.0.0:*    
    LISTEN  0       0               0.0.0.0:41853           0.0.0.0:*
    State   Recv-Q  Send-Q    Local Address:Port       Peer Address:Port  
    LISTEN  0       0             127.0.0.1:58505           0.0.0.0:*    
    LISTEN  0       0             127.0.0.1:46253           0.0.0.0:*    
    LISTEN  0       0         192.168.122.1:domain          0.0.0.0:* 
    

    因此我的 nc 绑定到端口 41853,随机选择

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    相关资源
    最近更新 更多