Building a TCP Echo Server
Walk through a complete TCP echo server implementation.
Goal
Build a minimal TCP server that accepts a connection, reads bytes, and writes them back. The "Hello World" of network programming.
Server Skeleton
The server flow: create socket, bind, listen, accept, handle. Loop forever.
int srv = socket(AF_INET, SOCK_STREAM, 0);
bind(srv, (sockaddr*)&addr, sizeof(addr));
listen(srv, 10);
while (true) {
int client = accept(srv, nullptr, nullptr);
handle(client);
close(client);
}All lessons in this course
- BSD Sockets API in C++
- Building a TCP Echo Server
- Asynchronous IO with epoll and kqueue
- Using Boost Asio for Modern Async IO