Skip to content

Last updated:2026-10-12

C++20 Coroutine HTTP Server

A Modern C++ HTTP Server

Recently members of the Knowledge Planet community kept asking me whether I had any network-related projects, so I wrote a complete project based on modern C++ and managed with CMake.

The project has the following features:

  • Extensive use of modern C++ features
  • C++20 related
    • cortouine
    • concept
    • std::format
    • std::span
  • C++17 related
    • if statement initializers
    • structured bindings
    • optional
  • Efficient event-driven mechanism
  • Uses epoll to implement the event loop and manage large numbers of client connections. epoll is an efficient tool for handling high-concurrency I/O on the Linux platform; it can listen for events on multiple file descriptors simultaneously, effectively reducing system call overhead and improving the concurrency of I/O operations.
  • The server listens for connection events via non-blocking epoll_wait in the main event loop, avoiding the performance bottlenecks of traditional blocking I/O.
  • Coroutines and asynchronous programming
  • Adopts C++20 coroutines to simplify the implementation of asynchronous tasks, providing a way of writing that resembles synchronous code and making the logic clearer. The project uses co_await to asynchronously read and write client data, reducing the overhead of thread context switching.
  • Implements the two classes SocketAwaiter and AsyncSocketOperation to encapsulate asynchronous read and write operations on sockets, making it much simpler and more natural to write asynchronous processing code.
  • Asynchronous handling of file I/O operations
  • The server supports asynchronously reading the contents of HTML files. Asynchronous file reading is implemented with std::async to avoid blocking the main thread, so client requests can be responded to faster.
  • When handling requests, the required files are read asynchronously and cached in memory, reducing the impact of I/O operations on performance.
  • Non-blocking socket operations
  • Uses fcntl to set sockets to non-blocking mode, ensuring that accept and read/write operations do not block the server's main thread. This is especially important in high-concurrency scenarios and can significantly improve the server's concurrent processing capability.

Project preview:

How to learn: detailed documentation + code are available; you can purchase it separately via the WeChat below or join the Knowledge Planet community.

Comments