Socket send concurrency guarantees

If I have one socket shared between two processes (or two threads), and in both of them I try to send a large message (larger than the buffer of the supporting protocol) that blocks, is it guaranteed that both messages will be sent sequentially? Or is it possible that messages will alternate within the kernel?

I am mainly interested in the behavior of TCP over IP, but it would be interesting to know if it changes according to the socket protocol.

+2
source share
3 answers

You ask that if you are write()message A, then B is on the same socket, is A guaranteed to B? For SOCK_STREAM (e.g. TCP) and SOCK_SEQPACKET (almost never used) sockets, the answer is unqualified yes. For SOCK_DGRAM over the Internet (i.e. UDP packets), the answer is no: packets can be reordered by the network. On one host, the unix domain datagram socket (on all systems that I know) will keep order, but I do not believe that this is guaranteed by any standard, and I am sure there are extreme cases.

: , , , ? : (write/writev/sendto/sendmsg) . , , , .

+4

UDP, , . IP, MTU, . , UDP, , UDP ( , ..).

TCP, , . " , - ?" .

, , (mutex) , .

TCP io. , . recv() , .

+1

STREAM, , , - ( , ), , .

, ( ) , , .

If you send messages to DGRAM sockets, on the other hand, either the entire message will be sent atomically (as a packet with one layer 4, which can be fragmented and reassembled by the lower levels of the protocol stack), or you will get an error ( EMSGSIZElinux or other options UNIX)

0
source

All Articles