Skip to main content

Network Programming

The Berkeley sockets API is the foundation of network programming on Unix-like systems. Every web server, database client, chat application, and distributed system you have ever used communicates through this API (or a thin wrapper around it). It was designed in the early 1980s at UC Berkeley and has barely changed since — a testament to how well the abstraction was designed.

Socket Fundamentals


TCP Server


TCP Client


Modern Address Resolution (getaddrinfo)


UDP Server and Client


Non-Blocking I/O


I/O Multiplexing with select()


I/O Multiplexing with epoll (Linux)

select() has a fatal flaw: it scans ALL file descriptors every time, making it O(n) per call. With 10,000 connections, most of which are idle, this wastes enormous CPU time. epoll solves this by only returning the file descriptors that actually have events — making it O(active_connections) instead of O(total_connections). This is why nginx and Redis use epoll and can handle hundreds of thousands of concurrent connections on a single thread.

Socket Options


HTTP Server Example


Exercises

1

Chat Server

Build a multi-client chat server where messages are broadcast to all connected clients.
2

File Transfer

Implement a simple file transfer protocol with upload/download commands.
3

HTTP Client

Build an HTTP client that can make GET requests and handle chunked transfer encoding.
4

High-Performance Server

Build a server using epoll that can handle 10,000+ concurrent connections.

Next Up

Build a Shell

Apply your knowledge by building a Unix shell