Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

example to explain unix domain socket - AF_INET vs AF_UNIX

Tags:

while I was reading for what AF_INET means, I learned that there is another family called UNIX domain socket. Here is the wiki link I read about this.

I do not understand what this means:

Unix domain sockets use the file system as their address name space. They are referenced by processes as inodes in the file system. This allows two processes to open the same socket in order to communicate. However, communication occurs entirely within the operating system kernel.

If I want to do SSH or FTP, what family do I use AF_INET or AF_UNIX. I am actually confused here a bit.

like image 325
eagertoLearn Avatar asked Jan 09 '14 22:01

eagertoLearn


People also ask

What is the difference between the domains Af_inet and AF_UNIX?

The difference is that an INET socket is bound to an IP address-port tuple, while a UNIX socket is "bound" to a special file on your filesystem. Generally, only processes running on the same machine can communicate through the latter.

What is Af_inet in socket?

AF_INET is an address family that is used to designate the type of addresses that your socket can communicate with (in this case, Internet Protocol v4 addresses). When you create a socket, you have to specify its address family, and then you can only use addresses of that type with the socket.

How does Unix domain socket work?

Unix sockets are bidirectional. This means that every side can perform both read and write operations. While, FIFOs are unidirectional: it has a writer peer and a reader peer. Unix sockets create less overhead and communication is faster, than by localhost IP sockets.

What is Unix domain socket path?

UNIX domain sockets are named with UNIX paths. For example, a socket might be named /tmp/foo. UNIX domain sockets communicate only between processes on a single host.


2 Answers

If you want to communicate with a remote host, then you will probably need an INET socket.

The difference is that an INET socket is bound to an IP address-port tuple, while a UNIX socket is "bound" to a special file on your filesystem. Generally, only processes running on the same machine can communicate through the latter.

So, why would one use a UNIX socket? Exactly for the reason above: communication between processes on the same host, being a lightweight alternative to an INET socket via loopback.

In fact, INET sockets sit at the top of a full TCP/IP stack, with traffic congestion algorithms, backoffs and the like to handle. A UNIX socket doesn't have to deal with any of those problems, since everything is designed to be local to the machine, so its code is much simpler and the communication is faster. Granted, you will probably notice the difference only under heavy load, e.g. when reverse proxying an application server (Node.js, Tornado...) behind Nginx etc.

like image 137
Stefano Sanfilippo Avatar answered Sep 19 '22 04:09

Stefano Sanfilippo


AF_UNIX Sockets provide for great inter-process communication. Open a socket pair socketpair(..)" and bind to a temporary filename. Write to one of the pair arrives at the other. The kernel routes messages without protocol or filesystem overhead. One can use blocking i/o or select(...) to synchronize threads and processes in a FIFO manner. I like non-blocking with select and datagram (can get a length) mode but you can choose your own. Be sure to delete the temporary file on exit (it will have zero bytes but will still show up in the filesystem directory)

like image 36
jlp Avatar answered Sep 22 '22 04:09

jlp