gpt4 book ai didi

c - 在 Linux 下,recv 是否可以在 UDP 上返回 0?

转载 作者:IT王子 更新时间:2023-10-29 00:27:36 28 4
gpt4 key购买 nike

我刚刚清理了我们不久前编写的一些代码,并注意到对于 udp 套接字,0 被视为连接已关闭。

我很确定这是从等效的 tcp 版本移植相同的 recv 循环的结果。但这让我感到奇怪。 recv可以为udp返回0吗?在 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.

关于c - 在 Linux 下,recv 是否可以在 UDP 上返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12505892/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com