簡體   English   中英

使用 UNIX sockets “零長度數據報”的原因是什么?

[英]What is the reason for using UNIX sockets “zero-length datagrams”?

recv()手冊頁中,我發現在這種情況下返回值可以為零:

各種域(例如,UNIX 和 Internet 域)中的數據報 sockets 允許零長度數據報。 當接收到這樣的數據報時,返回值為 0。

何時或為什么應該在 UNIX 套接字互通中使用零長度數據報 它的目的是什么?

一種這樣的用途是在您希望關閉 UDP 服務線程時取消阻止 recvfrom() 調用 - 設置“終止”標志並在 localhost 堆棧上發送零長度數據報。

我昨天在研究另一個問題的答案時偶然發現了一個例子。

在用於獲取遠程服務器當前時間的舊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 版本只需要連接到服務器),服務器必須接收數據報以提醒用戶正在請求時間(並知道要向哪個地址發送回復) )。 該數據報的內容被忽略,因此不妨指定它應該為空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM