Skip to main content

Sockets

Sockets are C structures which refer to either server accepting network connections or clients which initiate network connections.

Socket Multiplexing

Whenever there is an 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 due to its dynamic nature.

This is because network interfaces can be dynamically created, configured, or removed, reflecting changes in network topology or virtual networking setups. This dynamic nature doesn't fit well with the static model of device files under /dev.

Unix Domain Sockets

The socket structures what we speak here is used not only for network communication but also for inter-process communication (IPC) on the same host via Unix Domain Sockets (UDS).

UDS is exactly same as IP but it uses file system path as address instead of IP address and port number.

Virtual File System (VFS) and Sockets

When ever a socket is created, a .sock file is created in the file system.

This file has special kernel methods associated with it to handle read and write. These methods just read and write data between different memory buffers of two different processes.

File Descriptors for Sockets

Normally for regular file operations, there is just one file descriptor created for a file which is used for both reading and writing.

But in case of sockets, the kernel creates two file descriptors. One for reading and one for writing. This means, the producer gets one file descriptor and the consumer gets the another file descriptor to which it can write responses to.

why do sockets have two file descriptors?

Sockets are simply backed by buffers in memory. So when a single buffer is used for both reading and writing, then same process will write and also read from the same buffer which is useless.