简体   繁体   中英

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. 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?

This StackOverflow question is similar but in the context of a non-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. Beej's guide asserts that it may happen. libuv asserts that it cannot. POSIX does not seem to forbid it.

Therefore, I am curious what circumstances, if any, lead to a "short write" return value for UDP sockets on Linux? Is there an internet RFC or clause in POSIX which forbids it? Can this occur, for example, if a UDP datagram is fragmented (sent in multiple IP packets)?

UDP is a datagram protocol, ie it is not a byte stream as TCP but there are clear message boundaries inside the protocol. These message semantics are reflected when reading, ie a single recv will return a single (and full) message.

A single message (datagram) is send with a single send and there is no other way to specify the message boundary. But a "short send" would introduce message boundaries where they don't belong and thus should not be possible for UDP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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