linux example vs - Difference between UNIX domain STREAM and DATAGRAM sockets?

2 Answers

The main difference is that one is connection based (STREAM) and the other is connection-less (DGRAM) - the difference between stream and packet oriented communication is usually much less important.

With SOCK_STREAM you still get all the connection handling, i.e. listen/accept and you can tell if a connection is closed by the other side.

Note that there is also a SEQPACKET socket type that's still connection oriented, but preserves message boundaries (which might save you from implementing a message-oriented layer on top of a STREAM socket).

I would expect data transfer performance to be similar for all of these types, the main difference is just what semantics you want.

pipe path tcp

This question is NOT for the difference between STREAM type and DATAGRAM type INTERNET sockets. I know that STREAM sockets use TCP, Datagram sockets use UDP and all the TCP,UDP stuff, packets arriving in order, ACK, NACK etc. I understand the importance of these over internet.

Q1) When I create a UNIX domain socket which is a local socket, how would it matter if the socket is STREAM socket or DATAGRAM socket. This type of socket would write the data to the socket file, would the protocol matter in this case since I am not transmitting data over a network? Is there any chance of data loss in this case if I use UNIX-based DATAGRAM sockets?

Q2) Does UNIX DATAGRAM sockets provide better performance than UNIX STREAM sockets?

Q3) How to decide for a STREAM/DATAGRAM UNIX based socket in my application?


If the clients and servers will always be on the same machine and the goal is to have minimal latency and maximum bandwidth, use shared memory.