UNIX Domain Socket¶
UNIX domain sockets provide local inter-process communication between processes on the same Linux system.
They use the socket API, but traffic does not leave the host. This makes them useful for local services, daemon control channels, and structured IPC.
What Problem It Solves¶
Use UNIX domain sockets when two local processes need a socket-like communication channel without using TCP/IP networking.
Typical use cases:
- local client/server communication
- daemon control sockets
- request/response protocols
- stream or datagram IPC
- file descriptor passing with
SCM_RIGHTS
Core Concepts¶
| Concept | Description |
|---|---|
AF_UNIX / AF_LOCAL |
Address family for local IPC sockets |
SOCK_STREAM |
Connection-oriented byte stream |
SOCK_DGRAM |
Message-oriented datagram socket |
struct sockaddr_un |
UNIX socket address structure |
| pathname socket | Socket bound to a filesystem path |
| abstract socket | Linux-specific socket address not stored in the filesystem |
Key APIs¶
socket(AF_UNIX, ...)bind()listen()accept()connect()send()/recv()sendmsg()/recvmsg()for ancillary data
Learning Path¶
- Start with basic local client/server communication.
- Compare stream and datagram behavior.
- Add structured message framing.
- Use
sendmsg()andrecvmsg()when file descriptors need to be transferred.
Common Pitfalls¶
Warning
A pathname UNIX socket remains in the filesystem after the process exits unless it is removed with unlink().
Note
UNIX domain sockets are still file descriptors, so they can be integrated into poll() or epoll() event loops.