I need to build a server using Unix domain sockets, and it looks that there are several options to choose the kind of communication.
From man 2 socket:
- SOCK_STREAM
- SOCK_DGRAM
- SOCK_SEQPACKET
So, for what is better suited every one of them? (stream, datagram, packet)
It really depends what kind of server you are going to implement.
If message boundaries are important, then
SOCK_DGRAMwould be the best choice.Because
recvfrom/recvmsg/selectwill return when a complete message is received.With
SOCK_STREAM, message receiving is more tricky: One receiving call may return a partial message, or part of two messages, or several messages… etc.If message boundaries are not important, then
SOCK_STREAMcould be the best choice.SOCK_DGRAMofAF_INETis unreliable UDP. But, in most sytems,SOCK_DGRAMofAF_UNIXis reliable.For example: If queue of receiver is full, sender will be blocked until there is space.