且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C语言中的非阻塞udp套接字编程:我能得到什么?

更新时间:2022-03-22 08:25:48

最后,找个借口从我的旧办公室里掏出我的史蒂文斯书.

Finally, an excuse to dig out my Stevens books from my old office boxes.

提供的缓冲区足够大,标准的Berkeley套接字recv()recvfrom()函数将永远不会返回部分数据报.在内核完全接收并重新组装数据报之前,该数据报对应用程序不可用.

Provided the buffer is large enough, the standard Berkeley sockets recv() and recvfrom() functions will never return a partial datagram. The datagram is not available to the application until the kernel has completely received and reassembled the datagram.

有趣的是,今天这已不是什么大问题了,当所提供的缓冲区太小时,其他网络编程接口就无法达成共识:

Interestingly, and this isn't much (any?) of an issue today, other network programming interfaces don't agree on the behavior when the provided buffer is too small:

套接字API的传统Berkeley版本会截断数据报,并丢弃所有多余的数据.是否通知应用程序取决于版本. (4.3BSD Reno和更高版本可以通知应用程序该数据报已被截断.)

The traditional Berkeley version of the sockets API truncates the datagram, discarding any excess data. Whether the application is notified depends on the version. (4.3BSD Reno and later can notify the application that the datagram was truncated.)

SVR4(包括Solaris 2.x)下的套接字API不会截断数据报.任何多余的数据将在后续读取中返回.不会通知应用程序从单个UDP数据报中正在执行多次读取.

The sockets API under SVR4 (including Solaris 2.x) does not truncate the datagram. Any excess data is returned in subsequent reads. The application is not notified that multiple reads are being fulfilled from a single UDP datagram.

TLI API不会丢弃数据.而是返回一个标志,指示有更多数据可用,并且应用程序随后的读取将返回数据报的其余部分.

The TLI API does not discard the data. Instead a flag is returned indicating that more data is available, and subsequent reads by the application return the rest of the datagram.

(Stevens,TCP/IP图解,第1卷,第160页)

(Stevens, TCP/IP Illustrated, Volume 1, p. 160)