简体   繁体   中英

Difference between Unix domain SOCK_DGRAM and SOCK_SEQPACKET?

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?

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, ie to send some notifies.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM