"Concurrency is not a luxury, it is a system requirement."
A precision-engineered, from-scratch implementation of a scable TCP echo server in pure C. Built with a focus on systems programming excellence, manual memory management, and high-performance I/O multiplexing via epoll.
Modern networking libraries often abstract away the visceral mechanics of the socket API and the kernel's event notification systems. tcp-server-c strips away the abstraction. It is designed to serve as a reference implementation for how high-concurrency systems manage state, memory, and I/O during the "C10k" challenge.
- Linux Native Scability: Leverages
epollin Edge-Triggered mode for O(1) event notification. - Strict Lint Compliance: Zero-warning policy with
cppcheck,clang-tidy, andclang-format. - Memory Integrity: Verified leak-free performance via
valgrindand AddressSanitizer. - Robust Error Handling: Defensive programming at the socket level to handle network instability and signal interruptions.
The codebase is strictly modular, separating the concerns of the socket layer, the event loop, and the connection state machine.
graph TD
Main[main.c: Entry Point] --> Signals[Signal Handling: Graceful Shutdown]
Main --> Server[server.c: Core Loop]
Server --> Epoll[epoll_handler.c: I/O Multiplexing]
Server --> SocketOps[socket_ops.c: Sockets/Binding]
Server --> Conn[connection.c: State Machine]
Conn --> Data[Read/Write Buffers]
Data --> Process[Echo Logic]
| Feature | Implementation | Engineering Note |
|---|---|---|
| Multiplexing | epoll |
Scalable I/O handling for thousands of concurrent clients. |
| Edge Triggering | EPOLLET |
Minimizes kernel-to-user-space context switches. |
| State Tracking | connection_t |
Per-socket read/write buffering and metadata. |
| Non-blocking I/O | O_NONBLOCK |
Prevents loop starvation during partial I/O operations. |
| Clean Shutdown | SIGINT/SIGTERM |
Automated resource reclamation and socket closure. |
The project includes an extensive educational curriculum documentation in docs/:
- Socket Fundamentals: Understanding descriptors, binding, and the listen/accept handshake.
- TCP Protocol Layer: The mechanics of sliding windows, buffering, and flow control.
- Server Lifecycle: Managing the transition from initialization to steady state.
- I/O Multiplexing: Why
epollsucceeds whereselectandpollfail. - Memory & Buffering: Efficient buffer compaction and avoidance of fragments.
- Error Handling: Recovering from broken pipes and transient failures.
Verified components of the development lifecycle:
- Linter:
clang-tidy&cppcheck(Strict mode) - Formatter:
clang-format(LLVM style) - Memory Check:
valgrind&ASan - Coverage:
gcov&lcov
Part of the implement-from-scratch series.