Tim
Tim

Reputation: 99428

Is there only one Unix Domain Socket in the communication between two processes?

There are two kinds of sockets: network sockets and Unix Domain Sockets.

When two processes communicate using network sockets, each process creates its own network socket, and the processes communicate by connection between their sockets. There are two sockets, each belonging to a different process, being the connection endpoint of each process

When two processes communicate using Unix Domain sockets, a Unix Domain socket is addressed by a filename in the filesystem.

Upvotes: 4

Views: 1594

Answers (1)

user2404501
user2404501

Reputation:

There are two sockets, one on each end of the connection. Each of them, independently, may or may not have a name in the filesystem.

The thing you see when you ls -l that starts with srwx is not really "the socket". It's a name that is bound to a socket (or was bound to a socket in the past - they don't automatically get removed when they're dead).

An analogy: think about TCP sockets. Most of them involve an endpoint with a well-known port number (22 SSH; 25 SMTP; 80 HTTP; etc.) A server creates a socket and binds to the well-known port. A client creates a socket and connects to the well-known port. The client socket also has a port number, which you can see in a packet trace (tcpdump/wireshark), but it's not a fixed number, it's just some number that was automatically chosen by the client's kernel because it wasn't already in use.

In unix domain sockets, the pathname is like the port number. If you want clients to be able to find your server socket, you need to bind it to a well-known name, like /dev/log or /tmp/.X11-unix/X0. But the client doesn't need to have a well-known name, so normally it doesn't do a bind(). Therefore the name /tmp/.X11-unix/X0 is only associated with the server socket. You can confirm this with netstat -x. About half the sockets listed will have pathnames, and the other half won't. Or write your own client/server pair, and call getsockname() on the client. Its name will be empty, while getsockname() on the server gives the pathname.

The ephemeral port number automatically assigned to a TCP client has no counterpart in unix domain socket addresses. In TCP it's necessary to have a local port number so incoming packets can be matched to the correct socket. Unix domain sockets are linked directly in their kernel data structures, so there's no need. A client can be connected to a server and have no name.

And then there's socketpair() which creates 2 unix domain sockets connected to each other, without giving a name to either of them.

(Not mentioned here, and not really interesting: the "abstract" namespace.)

Upvotes: 3

Related Questions