Skip to main content

Sockets

Sockets are C structures. They refer to a server that accepts connections or a client that starts them.

Socket Multiplexing

On each incoming request, the following high-level steps happen.

socket-process
sockets-multithread

Network adapters not listed in /dev

Network interfaces aren't listed under /dev because they're dynamic.

They can be created, configured, or removed at any time. This tracks changes in the network topology or virtual setups. Such a dynamic nature doesn't fit the static model of device files under /dev.

Unix Domain Sockets

These socket structures aren't only for network communication. They also handle inter-process communication (IPC) on the same host. This uses Unix Domain Sockets (UDS).

UDS is just like IP. But it uses a file system path as the address, not an IP address and port.

Virtual File System (VFS) and Sockets

Whenever a socket is created, a .sock file appears in the file system.

This file has special kernel methods for read and write. They just move data between the memory buffers of two processes.

File Descriptors for Sockets

For regular files, there is just one file descriptor. It's used for both reading and writing.

For sockets, though, the kernel creates two file descriptors. One is for reading. One is for writing. The producer gets one. The consumer gets the other to write responses to.

why do sockets have two file descriptors?

Sockets are backed by buffers in memory. With one shared buffer, the same process would write and read from it. That's useless.