Networking in C · advanced · ~12 min

select / poll for multiplexing

Handle multiple sockets in one thread.

Lesson

select(int nfds, fd_set *r, fd_set *w, fd_set *e, struct timeval *to) waits for any of several fds to become readable/writable, with an optional timeout. poll is the modern, cleaner variant with an array of struct pollfd.

These let a single thread serve many connections without fork per client. The next step up is epoll (Linux) / kqueue (BSD/macOS) — better for thousands of fds.

Common mistakes

  • Forgetting to refill the fd_set every iteration — select modifies it.