在Linux下,recv会在UDP上返回0吗?

我只是清理一些我们写的代码,注意到对于一个udp套接字,0被视为关闭的连接.

我相当肯定这是从等效的tcp版本移植相同的recv循环的结果.但这让我很奇怪.可以recv返回0为udp?在tcp上,信号通知另一端已经关闭了连接. udp没有连接的概念,所以它可以返回0?如果可以的话,这是什么意思?

注意:linux中的手册页没有区分udp和tcp的返回码为零,这可能是为什么我们保留在代码中的检查.

解决方法

udp doesn’t have the concept of a connection so can it return 0? and
if it can,what is it’s meaning

这意味着收到一个0长的数据报.从伟大的UNP

Writing a datagram of length 0 is acceptable. In the case of UDP,this
results in an IP datagram containing an IP header (normally 20 bytes
for IPv4 and 40 bytes for IPv6),an 8-byte UDP header,and no data.
This also means that a return value of 0 from recvfrom is acceptable for a datagram protocol: It does not mean that the peer has closed the connection,as does a return value of 0 from read on a TCP socket. Since UDP is connectionless,there is no such thing as closing a UDP connection.

相关文章

文章浏览阅读1.8k次,点赞63次,收藏54次。Linux下的目录权限...
文章浏览阅读1.6k次,点赞44次,收藏38次。关于Qt的安装、Wi...
本文介绍了使用shell脚本编写一个 Hello
文章浏览阅读1.5k次,点赞37次,收藏43次。【Linux】初识Lin...
文章浏览阅读3k次,点赞34次,收藏156次。Linux超详细笔记,...
文章浏览阅读6.8k次,点赞109次,收藏114次。【Linux】 Open...