0
votes

I am sending N bytes from a unix domain socket (AF_UNIX, SOCK_DGRAM) to another. However, if I read X bytes from the other socket, where X < N, a subsequent call to read() blocks and I cannot get the rest of the N-X bytes.

Is this an expected behaviour for unix domain sockets? Is the rest of the N-X bytes thrown by the kernel. Is there a solution, such as a socket option?

1
What are those sockets - UDP or TCP?SergeyA
The socket I use is (AF_UNIX, SOCK_DGRAM)Benji Mizrahi

1 Answers

3
votes

The behaviour you are observing is not specific to AF_UNIX sockets. It is specific to SOCK_DGRAM sockets. The distinctive property of datagram sockets is that they are message-oriented.

Unlike TCP sockets, every time you call send() or sendto() on a datagram socket, you are creating a single message. You should read the whole message with a single recv() or recvfrom() call. Whatever was not read, is discarded, and next call to recv() will give you the next message on the wire.

If stream-oriented communication is desired, SOCK_STREAM should be used instead.