加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-13 18:23:53 所属栏目:Linux 来源:网络整理
导读:我只是清理一些我们写的代码,注意到对于一个udp套接字,0被视为关闭的连接. 我相当肯定这是从等效的tcp版本移植相同的recv循环的结果.但这让我很奇怪.可以recv返回0为udp?在tcp上,信号通知另一端已经关闭了连接. 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读