在Linux下,可以在UDP的recv再回到0? [英] Under Linux, can recv ever return 0 on UDP?

查看:418
本文介绍了在Linux下,可以在UDP的recv再回到0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是清理一些code,我们写了一段时间后,发现了一个UDP套接字,0被作为连接关闭处理。

我敢肯定,这是从移植相当于TCP版本相同的recv循环的结果。但它使我怀疑。可以为RECV UDP返回0?在TCP它标志着另一端已经关闭了连接。 UDP没有连接的概念,所以可以将它返回0?如果可以,那是什么的意思?

注意:在Linux手册页不区分UDP和TCP的零返回code这可能是为什么我们一直在code中的检查


解决方案

  

UDP不具有连接的概念,以便可以将它返回0?和
  如果可以,有什么事的意思


这意味着一个长度为0的数据报已被接收。从大 UNP


  

写长度为0的数据报是可以接受的。在UDP,这种情况下
  结果在一个IP数据报包含的IP报头(通常20字节
  IPv4和40字节的IPv6),一个8字节的UDP头,并且没有数据。
  这也意味着,从recvfrom的0返回值是可以接受的
  对于数据报协议
:这并不意味着对已关闭
  连接一样,0返回值从TCP套接字读取。
  由于UDP是无连接的,有作为关闭UDP没有这样的事情
  连接。


I'm just cleaning up some code we wrote a while back and noticed that for a udp socket, 0 is being treated as the connection closed.

I'm quite sure this was the result of porting the same recv loop from the equivalent tcp version. But it makes me wonder. Can recv return 0 for udp? on tcp it signals the other end has closed the connection. udp doesn't have the concept of a connection so can it return 0? and if it can, what is it's meaning?

Note: the man page in linux does not distinguish udp and tcp for a return code of zero which may be why we kept the check in the code.

解决方案

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

It means a 0-length datagram was received. From the great 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.

这篇关于在Linux下,可以在UDP的recv再回到0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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