2

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?

RTC222
  • 2,025
  • 1
  • 20
  • 53
  • 1
    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 May 02 '20 at 16:31
  • 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 May 02 '20 at 16:36
  • 1
    do you understand the difference between a *stream* and a *packet/message* ? – bruno May 02 '20 at 16:38
  • I do, and if that's the only difference then that answers my question. I was concerned that SEQPACKET was presented as more reliable than DGRAM, but it appears that they are equally reliable, at least in the Unix domain. – RTC222 May 02 '20 at 16:44
  • 1
    it is not written SOCK_SEQPACKET is always reliable , may be it is not and you can loose packet – bruno May 02 '20 at 16:46
  • 1
    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 May 02 '20 at 16:54
  • this is my understanding. – bruno May 02 '20 at 16:56
  • `SOCK_DGRAM` is *not* a stream, @bruno. It is message(datagram)-oriented, just like `SOCK_DGRAM`. This is the significance of "preserves message boundaries", which is the primary discriminator between message-oriented and stream-oriented behavior. – John Bollinger May 02 '20 at 18:46

2 Answers2

3

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.

Erki Aring
  • 2,032
  • 13
  • 15
0

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.

Yashasvi.G
  • 43
  • 7