我只是清理一些我们写的代码,注意到对于一个udp套接字,0被视为关闭的连接.
我相当肯定这是从等效的tcp版本移植相同的recv循环的结果.但这让我很奇怪.可以recv返回0为udp?在tcp上,信号通知另一端已经关闭了连接. udp没有连接的概念,所以它可以返回0?如果可以的话,这是什么意思?
解决方法
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.