使用UNIX套接字“零长度数据报”的原因是什么?

4
recv()的手册页面中,我发现在以下情况下返回值可以为零:
各种域(例如UNIX和Internet域)中的数据报套接字允许零长度数据报。当收到这样的数据报时,返回值为0。
在UNIX套接字互通中,何时或为什么应该使用零长度数据报?它的目的是什么?
2个回答

2

其中一种用途是在您希望关闭UDP服务线程时解除recvfrom()调用的阻塞 - 设置“终止”标志并在本地主机堆栈上发送零长度数据报。


这只适用于DGRAM套接字吗?还是可以与SOCK_STREAMSOCK_SEQPACKET一起使用? - NK-cell

1

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

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

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一起使用? - NK-cell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接