Course contentsShow
← Computer Science
Lesson 1549 of 2,87235. Application Layer ProtocolsFree lesson

Non-Blocking Sockets and I/O Multiplexing

Learn about non-blocking socket modes and modern I/O multiplexing techniques like poll and epoll.

Non-Blocking Sockets and I/O Multiplexing

What you'll learn: How to handle multiple socket connections efficiently without blocking your program's execution.

The Problem with Blocking Sockets

By default, socket operations like accept(), recv(), and send() are blocking—they pause your program until data arrives or space becomes available. When handling multiple clients, this creates a bottleneck: while you're waiting for one client, others must wait their turn.

Non-Blocking Mode

Setting a socket to non-blocking mode means operations return immediately, even if they can't complete. Instead of waiting, they return an error code (like EWOULDBLOCK) indicating "not ready yet." Your program can then do other work and check back later.

Think of it like checking your mailbox: blocking mode means standing there until mail arrives; non-blocking means peeking in, and if it's empty, doing something else before checking again.

I/O Multiplexing: poll and epoll

Manually checking non-blocking sockets is inefficient. I/O multiplexing mechanisms like poll() and epoll() solve this elegantly:

  • poll(): You give it a list of sockets to monitor. It waits until one or more become ready for reading/writing, then tells you which ones.
  • epoll() (Linux): A more scalable version of poll(), efficient even with thousands of connections. Instead of passing the entire socket list every time, you register sockets once and receive only the ready ones.

These tools let one thread monitor many sockets simultaneously, only processing those with activity—far more efficient than the select() approach you learned earlier, especially at scale.

Key Takeaway: Non-blocking sockets prevent operations from stalling your program, while poll() and epoll() efficiently monitor multiple sockets, enabling high-performance servers that handle thousands of concurrent connections without creating a thread per client.