Read without premium: Here
Unix sockets are a mechanism for facilitating inter-process communication (IPC) on the same machine. They provide a way for different processes to exchange data efficiently by leveraging the underlying operating system. While TCP/IP sockets enable communication over a network, Unix sockets are designed for local communication, and they do not incur the overhead of the network stack, making them faster when communicating within the same system.
Unix sockets are often used in various systems such as databases, web servers, and operating system utilities for communication between components running on the same machine. They are file-based, meaning that each socket has a path in the filesystem (like /tmp/my_socket.sock), and processes can use this path to establish communication.
How Do Unix Sockets Enable Communication?
Unix sockets enable bi-directional communication between processes. A process can listen for incoming connections using a socket, and another process can connect to that socket and send or receive data. This works similarly to how network sockets operate, except that Unix sockets don’t rely on IP addresses or ports.

Each application creates a Unix socket that acts as an endpoint for communication. The sockets allow applications to send and receive data.
The Unix sockets exist in the kernel space, where they facilitate communication between processes. The data sent from one application goes through its corresponding socket to the kernel, which then routes it to the destination socket of the other application.
Unix sockets come in two main types:
Stream Sockets (SOCK_STREAM)
Stream sockets provide a reliable, connection-oriented communication channel, similar to how TCP works in network communication. This means that:
Data is guaranteed to arrive in the correct order, without loss or duplication. The system handles retransmission in case of errors.
Before data can be sent, the two processes need to establish a connection. One process acts as a server (listening for connections), and the other acts as a client (initiating the connection).
Stream sockets treat the data as a continuous stream of bytes. There is no notion of individual messages. If one process sends 10 bytes followed by another 10 bytes, the receiving process will get 20 bytes in total, but it won’t know where the first message ends and the second one begins unless you design a protocol to handle that.
Stream sockets are ideal for use cases where you need to ensure that all data is received in the correct order and without any loss, such as file transfers, database communication, or other critical applications.
Datagram Sockets (SOCK_DGRAM)
Datagram sockets, on the other hand, provide a connectionless, message-oriented communication channel, similar to how UDP works in network communication. The key features of datagram sockets are:
Data is sent in distinct messages (datagrams). Each send operation corresponds to a single message, and each receive operation corresponds to a single message.
No connection is required between the processes before sending or receiving messages. You can send a message to a socket and the recipient can receive it without the overhead of establishing a connection.
There is no guarantee that messages will arrive, or that they will arrive in order, or that they won’t be duplicated. It’s up to the application to handle errors, acknowledgments, and retransmissions if necessary.
Datagram sockets are well-suited for scenarios where the overhead of establishing a connection is not desirable, and the communication is more about sending independent messages without needing guaranteed delivery. This is useful for situations where speed is prioritized over reliability, or the application can tolerate occasional loss of data.
Comparison with Other IPC Mechanisms:
Using Unix Sockets:
Server (Python):
import socket
import os
SOCKET_PATH = '/tmp/my_socket.sock'
if os.path.exists(SOCKET_PATH):
os.remove(SOCKET_PATH)
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind(SOCKET_PATH)
server.listen(1)
conn, _ = server.accept()
data = conn.recv(1024)
print(f"Received: {data.decode()}")
conn.close()
server.close()
os.remove(SOCKET_PATH)Client (Python):
import socket
SOCKET_PATH = '/tmp/my_socket.sock'
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client.connect(SOCKET_PATH)
client.sendall(b'Hello from Client')
client.close()The server code creates a Unix domain stream socket (AF_UNIX, SOCK_STREAM), binds it to a file (/tmp/my_socket.sock), and listens for a client connection. Once a client connects, the server accepts the connection and receives up to 1024 bytes of data. The received message is printed, and then both the connection and the server socket are closed. The socket file is also removed from the filesystem after use to clean up.
How to See Unix Sockets on Your Linux System?
Unix sockets are represented as special files on the filesystem, typically found in the /tmp or /var/run directories, though they can be located elsewhere. These files are created when processes bind to a Unix socket and are used to facilitate communication.
Unix sockets have a unique file type and can be viewed just like other files using the ls command. A socket file is usually identified by an "s" in the first column of the ls -l output, which indicates that it's a socket.
ls -l /tmpYou’ll see output like this if a socket file exists in /tmp:
srwxrwxrwx 1 ben ben 0 Oct 19 12:00 my_socket.sockThe “s” at the beginning of the permissions (srwxrwxrwx) indicates that this file is a Unix domain socket.
The netstat command can be used to list both network and Unix sockets that are currently active on the system. To specifically view Unix domain sockets, use the following command:
netstat -lxThis will display output like:
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 15666 /tmp/test_socket.sock
unix 2 [ ACC ] STREAM LISTENING 12345 /var/run/dbus/system_bus_socket- The
Pathcolumn shows the filesystem path to the Unix sockets. Typecan beSTREAM(for stream sockets) orDGRAM(for datagram sockets).LISTENINGindicates that a process is listening on that socket for incoming connections.




