</>EmbeddedInterview

Why does Linux have a separate socket API instead of using only open/read/write?

MediumLinux System Programming

Short answer: Sockets are file descriptors, so read(), write(), close(), poll() and epoll work on them. But networking needs things a file path cannot express: choosing a protocol family and type (TCP vs UDP vs raw, IPv4 vs IPv6 vs Unix vs CAN), binding to local addresses and ports, connecting to or accepting from remote endpoints that may not exist yet, per-message addresses for datagrams, and many options. The socket API (socket, bind, listen, accept, connect, sendto, recvfrom, setsockopt, shutdown) adds exactly those operations.

What the file API cannot express

  • Creation without a name: socket(AF_INET6, SOCK_STREAM, 0) creates an endpoint; there is nothing in the file system to open().
  • Addresses: a network peer is identified by an address and port (or a CAN ID, or a Bluetooth address), not a path. bind()/connect() take a generic struct sockaddr.
  • Connection set-up: listen()/accept() create a new descriptor for each incoming connection, a concept files do not have.
  • Datagram semantics: for UDP, each sendto() is one message with its own destination, and recvfrom() reports who sent each one. Message boundaries are preserved.
  • Options and control data: setsockopt() (buffer sizes, TCP_NODELAY, keepalive, multicast membership, timestamps) and ancillary data with sendmsg()/recvmsg() (passing file descriptors over Unix sockets, packet info).
  • Half-close: shutdown(fd, SHUT_WR) sends FIN but keeps reading.

The design choice still unifies a lot: because sockets are descriptors, they can be inherited across fork(), redirected by shells, passed to poll()/epoll() together with pipes, serial ports and timers, and closed with close().

Historical note: the API came from 4.2BSD (1983) and became the POSIX standard. Plan 9 took the opposite approach and represented network connections as files (/net/tcp/clone), showing it is possible; it never became mainstream.

Linux-specific families worth knowing in embedded work: AF_CAN (SocketCAN for CAN bus), AF_PACKET (raw Ethernet frames), AF_NETLINK (configuring the kernel: routes, interfaces), AF_UNIX (local IPC), AF_VSOCK (host ↔ VM communication).