【问题标题】:What is the reason for using UNIX sockets "zero-length datagrams"?使用 UNIX 套接字“零长度数据报”的原因是什么?
【发布时间】:2021-05-26 19:20:03
【问题描述】:

recv()'s 手册页中,我发现在这种情况下返回值可以为零:

各种域中的数据报套接字(例如,UNIX 和 Internet 域)允许零长度数据报。当这样的数据报 收到,返回值为0。

在 UNIX 套接字相互通信中何时或为什么应该使用 零长度数据报?它的目的是什么?

【问题讨论】:

    标签: c linux sockets unix unix-socket


    【解决方案1】:

    我昨天在研究另一个问题的答案时偶然发现了一个例子。

    在用于获取远程服务器当前时间的旧RFC 868 协议中,使用UDP 的工作流程如下:

    When used via UDP the time service works as follows:
    
       S: Listen on port 37 (45 octal).
    
       U: Send an empty datagram to port 37.
    
       S: Receive the empty datagram.
    
       S: Send a datagram containing the time as a 32 bit binary number.
    
       U: Receive the time datagram.
    
       The server listens for a datagram on port 37.  When a datagram
       arrives, the server returns a datagram containing the 32-bit time
       value.  If the server is unable to determine the time at its site, it
       should discard the arriving datagram and make no reply.
    

    在这种情况下,由于 UDP 的无连接特性(TCP 版本只需要连接到服务器)。该数据报的内容被忽略,因此不妨指定它应该为空。

    【讨论】:

    • 这仅适用于 DGRAM 套接字吗?或者这可以是SOCK_STREAMSOCK_SEQPACKET 的用户?
    【解决方案2】:

    当您希望关闭 UDP 服务线程时,其中一种用途是取消阻止 recvfrom() 调用 - 设置“终止”标志并在 localhost 堆栈上发送零长度数据报。

    【讨论】:

    • 这仅适用于 DGRAM 插槽吗?或者这可以是SOCK_STREAMSOCK_SEQPACKET 的用户?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多