C语言中的非阻塞udp套接字编程:我能得到什么? [英] non-blocking udp socket programming in C: what do I get?

查看:400
本文介绍了C语言中的非阻塞udp套接字编程:我能得到什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解从非blockig UDP套接字返回什么recv()/recvfrom()时遇到问题.

I have a problem in understanding what recv()/recvfrom() return from an non-blockig UDP socket.

与TCP相比更具体一些(如果我错了,请纠正我):

A bit more specific and compared to TCP (please correct me if I'm wrong):

  • 直到缓冲区中有一些数据,阻塞套接字(TCP或UDP)都不会从recv()返回.这可能是一定数量的字节(TCP)或完整的数据报(UDP).

  • A blocking socket (either TCP or UDP) won't return from a recv() until there is some data in the buffer. This could be some number of bytes (TCP) or a complete datagram (UDP).

非阻塞TCP套接字返回EWOULDBLOCK(linux)/WSAEWOULDBLOCK(windows)或缓冲区中当前的字节.由于TCP数据是流,因此返回多少字节无关紧要.

A non-blocking TCP socket either returns EWOULDBLOCK (linux) / WSAEWOULDBLOCK (windows) or the bytes that are currently in the buffer. As TCP data is a stream it doesn't matter how many bytes are returned.

现在的问题:

  • 如果没有可用数据,则非阻塞UDP套接字也将返回WOULDBLOCK(linux)/WSAEWOULDBLOCK(windows).但是,如果有数据可用,非阻塞UDP套接字是否只返回一些字节,这可能意味着您只能获得一半的数据报,或者UDP套接字是否总是返回完整的数据报??

数据报的一半"的意思是:如果套接字当前正在接收数据报的那一刻,我调用recv()会发生什么.在那一刻,缓冲区中有一些字节,但是数据报尚未完成.

What I mean with "half of a datagram" is: what happens if I call recv() in just the moment when the socket is currently receiving a datagram. In that moment there are some bytes in the buffer but the datagram isn't complete yet.

对您的解释和评论表示赞赏.谢谢!

Your explanations and comments are appreciated. Thanks!

推荐答案

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

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)

这篇关于C语言中的非阻塞udp套接字编程:我能得到什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆