Skip to content

implement-from-scratch/tcp-server-c

Repository files navigation

TCP Echo Server in Pure C

"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.


The Philosophy

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.

Key Engineering Pillars

  • Linux Native Scability: Leverages epoll in Edge-Triggered mode for O(1) event notification.
  • Strict Lint Compliance: Zero-warning policy with cppcheck, clang-tidy, and clang-format.
  • Memory Integrity: Verified leak-free performance via valgrind and AddressSanitizer.
  • Robust Error Handling: Defensive programming at the socket level to handle network instability and signal interruptions.

Architectural Blueprint

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]
Loading

Capabilities

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.

Technical Deep-Dives

The project includes an extensive educational curriculum documentation in docs/:

  1. Socket Fundamentals: Understanding descriptors, binding, and the listen/accept handshake.
  2. TCP Protocol Layer: The mechanics of sliding windows, buffering, and flow control.
  3. Server Lifecycle: Managing the transition from initialization to steady state.
  4. I/O Multiplexing: Why epoll succeeds where select and poll fail.
  5. Memory & Buffering: Efficient buffer compaction and avoidance of fragments.
  6. Error Handling: Recovering from broken pipes and transient failures.

Core Tooling

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.

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors