简体   繁体   English

Linux 上的 UDP 套接字上的 send、sendto 和 sendmsg 的可能返回值是多少?

[英]What are the possible return values for send, sendto and sendmsg on a UDP socket on Linux?

These functions return -1 on error and the input length on success.这些函数在错误时返回 -1,在成功时返回输入长度。 Under what conditions may they return a value less than the length and greater than or equal to 0 for an UDP socket on Linux?在什么情况下,对于 Linux 上的 UDP 套接字,它们可能返回小于长度且大于或等于 0 的值?

This StackOverflow question is similar but in the context of a non-Linux RTOS. 这个 StackOverflow 问题类似,但在非 Linux RTOS 的上下文中。 The answers assert it cannot happen, and the OS vendor contradicts them.答案断言它不可能发生,并且操作系统供应商与他们相矛盾。 This StackOverflow question is similar but the accepted answer only discusses receipt. 这个 StackOverflow 问题类似,但接受的答案只讨论收据。 Beej's guide asserts that it may happen. Beej 的指南断言它可能会发生。 libuv asserts that it cannot. libuv断言它不能。 POSIX does not seem to forbid it. POSIX 似乎并没有禁止它。

Therefore, I am curious what circumstances, if any, lead to a "short write" return value for UDP sockets on Linux?因此,我很好奇什么情况(如果有的话)会导致 Linux 上 UDP sockets 的“短写”返回值? Is there an internet RFC or clause in POSIX which forbids it? POSIX 中是否有禁止它的互联网 RFC 或条款? Can this occur, for example, if a UDP datagram is fragmented (sent in multiple IP packets)?例如,如果 UDP 数据报被分段(在多个 IP 数据包中发送),这种情况会发生吗?

UDP is a datagram protocol, ie it is not a byte stream as TCP but there are clear message boundaries inside the protocol. UDP 是一个数据报协议,即它不是一个字节 stream 就像 TCP 但协议内部有明确的消息边界。 These message semantics are reflected when reading, ie a single recv will return a single (and full) message.这些消息语义在阅读时会反映出来,即单个recv将返回单个(和完整)消息。

A single message (datagram) is send with a single send and there is no other way to specify the message boundary.单个消息(数据报)通过单个send ,没有其他方法可以指定消息边界。 But a "short send" would introduce message boundaries where they don't belong and thus should not be possible for UDP.但是“短发送”会在它们不属于的地方引入消息边界,因此对于 UDP 来说应该是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM