微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在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.

原文地址:https://www.jb51.cc/linux/401916.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐