LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2008, 04:03 PM   #1
tm007
LQ Newbie
 
Registered: Sep 2007
Posts: 9

Rep: Reputation: 0
Difference in UNIX Domain Sockets: SOCK_DGRAM vs SOCK_STREAM


I am writing some code using AF_UNIX sockets--AF_INET sockets is not an option. I'm trying to understand the differences between SOCK_STREAM and SOCK_DGRAM when using UNIX domain sockets--NOT AF_INET. My application will be using a simple protocol with defined headers so SOCK_DGRAM appears to the way to go to reduce reassembly of message on both sides of the connection. However I am not sure if there is something deep in the AF_UNIX implementation that will cause problems or sway my decision.

From the Linux Man pages it seems the reliability issues seen in INET SOCK_DGRAM sockets does not exist when using UNIX Domain (AF_UNIX) sockets. Therefore SOCK_STREAM and SOCK_DGRAM should be just as reliable. (referring to: http://linux.die.net/man/7/unix)

The only other difference I see is that AF_UNIX SOCK_DGRAM sockets support framing as they do for INET sockets. However I do not see anything that specifically states AF_UNIX SOCK_STREAM sockets are not framed based as well.

Does anyone know of other differences or other things to consider when selecting between AF_UNIX SOCK_DGRAM vs SOCK_STREAM?

Also does anyone know of a good reference for AF_UNIX sockets use? Information seems more limited with regards to AF_UNIX sockets.

Any help or suggestions are MUCH appreciated.

Last edited by tm007; 02-26-2008 at 04:06 PM. Reason: minor change
 
Old 02-26-2008, 04:10 PM   #2
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hi,

Basically SOCK_DGRAM is used for UDP packets, SOCK_STREAM for TCP. As far as reliability issues, it's UDP vs. TCP issues - no guarantee for UDP, guaranteed delivery for TCP.

Ciao!
 
Old 02-26-2008, 04:13 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Personally until this post I have never heard about AF_UNIX sockets.
Quote:
From the Linux Man pages it seems the reliability issues seen in INET SOCK_DGRAM sockets does not exist when using UNIX Domain (AF_UNIX) sockets.
Correct me if I have misunderstood, but AF_UNIX is for one machine so the reliability issue is not a concern here( I have never known a datagram to not be sent on a local machine).
 
Old 02-26-2008, 04:15 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Stream communication works exactly like a pipe once connected. Datagram communication doesn't ever connect, so each individual write operation stands alone from all others. I'm not sure exactly how it works, but if you're sending structures then it might be the way to go. info libc is a decent reference, but I really think you need to just experiment with each to see how they work with what you're trying to do.
ta0kira
 
Old 02-26-2008, 04:19 PM   #5
tm007
LQ Newbie
 
Registered: Sep 2007
Posts: 9

Original Poster
Rep: Reputation: 0
AF_INET supports datagrams over the Internet (i.e. UDP). AFUNIX is within the one machine, no external routing is done. I wonder if the AF_UNIX implementation could prioritize packets/messages differently for SOCK_DGRAM versus SOCK_STREAM but this is really part of my main question. Is there something different between the two socket types for UNIX Domain Sockets?
 
Old 02-26-2008, 04:24 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Unix sockets are just another form of Interprocess Communication ("IPCs"); they're analogous to "named pipes" on Windows. The one place I ever really *needed* them was a somewhat esoteric problem where we used an AF_UNIX stream to pass an AF_INET socket descriptor from one process to another process.

Anyway - if it doesn't have to be 100% reliable, and if your datagrams don't absolutely need to arrive in a specific order, then use datagrams. Otherwise, you'd probably be wise to use streams.

'Hope that helps .. PSM

PS:
Here's a link with a bit more info:
http://www.ecst.csuchico.edu/~beej/guide/ipc/usock.html
 
Old 02-26-2008, 07:02 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I'd say streams are more appropriate for data that has an indefinite size, and therefore may or may not have to be broken up into parts. The network vs. local issue doesn't have much to do with it really, since the difference is only in addressing. A datagram is more of a "putting a discrete object into a box" operation where another process checks that box if it's expecting something. I use local sockets with stream communication often, and it requires format generation on the send end and format parsing on the receiving end in order to work (unless you're just sending files.) In other words, to send anything more complex than line-formatted text you'll probably need to design a custom protocol (I use something similar to XML.) Are you passing a stream of data, or the contents of a structure?
ta0kira

Last edited by ta0kira; 02-26-2008 at 07:07 PM.
 
Old 02-27-2008, 05:01 AM   #8
tm007
LQ Newbie
 
Registered: Sep 2007
Posts: 9

Original Poster
Rep: Reputation: 0
I am essentially going to send "structs" of data. These sockets will communicate internally between a number of different SW components. The ordering and reliability of the data is important.

Quote:
Anyway - if it doesn't have to be 100% reliable, and if your datagrams don't absolutely need to arrive in a specific order, then use datagrams. Otherwise, you'd probably be wise to use streams.
According to the man pages I first referenced, the datagrams for UNIX domain sockets are as reliable as SOCK_STREAM and there is also no re-ordering issues. Therefore is there something else you are referring to? If so I assume this will also effect SOCK_STREAM for this implementation. Your comment is accurate for AF_INET sockets but I don't think it holds for AF_UNIX sockets.

Also, thanks for the reference, sadly it really says nothing about SOCK_DGRAM.
 
Old 02-27-2008, 11:25 AM   #9
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by tm007 View Post
According to the man pages I first referenced, the datagrams for UNIX domain sockets are as reliable as SOCK_STREAM and there is also no re-ordering issues.
Yes, that is correct. The ordering and reliability issues only manifest themselves in the INET domain, as a side-effect of the connectionless nature of the datagram sockets.

In fact, there is a third kind of reliability about concurrent writes to the same UNIX-domain, datagram-socket destination, which is that writes (i.e., sendtos) are atomically added to the destination’s receive buffer (at least in linux). This is stronger than the same thing using FIFO’s in which atomicity is guaranteed only in PIPE_BUF chunks at a time.

Also, a difference in implementation in the UNIX domain vs. INET domain is that on many systems, you should bind sockets in the UNIX domain, whereas in the INET domain, if you don’t explicitly bind, a random, unused port will be assigned.
 
Old 02-27-2008, 11:43 AM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by osor View Post
Also, a difference in implementation in the UNIX domain vs. INET domain is that on many systems, you should bind sockets in the UNIX domain, whereas in the INET domain, if you don’t explicitly bind, a random, unused port will be assigned.
How does that happen? When you listen and accept without bind first?
ta0kira
 
Old 02-27-2008, 12:02 PM   #11
tm007
LQ Newbie
 
Registered: Sep 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
In fact, there is a third kind of reliability about concurrent writes to the same UNIX-domain, datagram-socket destination, which is that writes (i.e., sendtos) are atomically added to the destination’s receive buffer (at least in linux).
So are writes using SOCK_STREAM also atomic?
 
Old 02-27-2008, 12:25 PM   #12
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by ta0kira View Post
How does that happen? When you listen and accept without bind first?
I meant in using datagram sockets in the UNIX domain, you need to bind, but in using datagram sockets in the INET domain, you don’t necessarily need to bind. I was not talking about stream sockets (sorry if that was not clear).
Quote:
Originally Posted by tm007 View Post
So are writes using SOCK_STREAM also atomic?
Do you mean concurrent writes? Concurrent writes will obviously have two separate connections connections, so there can’t be any overlap (the point is moot).
 
  


Reply

Tags
socket



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Large # of "Active UNIX domain sockets (w/o servers)" + network stalls jdavidow Linux - Networking 5 11-11-2007 10:05 AM
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-27-2007 09:49 AM
IPtables - Active UNIX domain sockets LordDaemon Linux - Networking 2 11-12-2006 05:29 AM
what's the difference between sockets? Thinking Programming 5 11-09-2005 11:44 AM
IPC (Message Queues vs. FIFOs vs. Unix-domain Sockets) TedMaul Programming 0 03-18-2004 04:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration