A Simple Echo Server
Serve many clients at once.
What We Are Building
An echo server accepts TCP connections and sends back whatever bytes a client sends. It is the "hello world" of network programming.
We will build it on epoll so a single thread serves many clients at once. The pieces are: a listening socket, the epoll loop, and per-client read-then-write handling.
Creating the Listening Socket
Start with socket() for an IPv4 TCP endpoint. The triple is AF_INET, SOCK_STREAM, and protocol 0.
This returns a file descriptor that we will bind, mark non-blocking, and eventually register with epoll. Check for -1 on every system call.
int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd == -1) { perror("socket"); exit(1); }All lessons in this course
- Blocking vs Non-Blocking I/O
- Setting Up epoll
- The Event Loop
- A Simple Echo Server