2
votes

According to the Linux man pages for Unix sockets, "Valid socket types in the UNIX domain are . . . SOCK_DGRAM, for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don't reorder datagrams); and (since Linux 2.6.4) SOCK_SEQPACKET, for a sequenced-packet socket that is connection-oriented, preserves message boundaries, and delivers messages in the order that they were sent." (http://man7.org/linux/man-pages/man7/unix.7.html).

I thought "always reliable and don't reorder datagrams" is the same as "delivers messages in the order that they were sent."

What's the practical difference between SOCK_DGRAM and SOCK_SEQPACKET?

2
socket SOCK_DGRAM are a stream, socket SOCK_SEQPACKET exchange packets (like SOCK_DGRAM so UDP except UDP is connection less and you may lost packets) - bruno
According to the man page quoted above, "SOCK_STREAM, for a stream- oriented socket; SOCK_DGRAM, for a datagram-oriented socket." I think the difference may include something more. - RTC222
do you understand the difference between a stream and a packet/message ? - bruno
it is not written SOCK_SEQPACKET is always reliable , may be it is not and you can loose packet - bruno
Very good point. So they both deliver messages in the order they were sent, but SOCK_SEQPACKET is not guaranteed to be reliable, as is SOCK_DGRAM. - RTC222

2 Answers

1
votes

In the context of UNIX domain sockets, the main difference between two is "datagram-oriented" vs "connection-oriented".

In case of SOCK_DGRAM you don't create a connection (to a server, for example), you just send packets to the server socket. And if server needs to reply, you need to create your own socket, make server aware of this socket and then server can send a reply to it. Very inconvenient, if you really need a connection, but can be useful when you just need one-way communication, i.e. to send some notifies.

SOCK_SEQPACKET is the way to go, when you need connection-oriented approach.

0
votes

The difference is better understood by the help of UDP and TCP. A protocol like UDP(connection-less) uses SOCK_DGRAM, implementation

A protocol like TCP(connection-oriented) uses SOCK_STREAM. However, even SOCK_SEQPACKET can be used. The difference between the two is very minimal, TCP can be implemented using the latter as well. In fact, SOCK_SEQPACKET is somewhat a hybrid of both. STCP is a use case for SOCK_SEQPACKET. Explained in this article: http://urchin.earth.li/~twic/Sequenced_Packets_Over_Ordinary_TCP.html

Here's a post that has discussed this in detail.