Minimal TCP server and client written in modern C++20 using POSIX sockets and Boost.Program_options.
- Server: accepts multiple clients, keepalive enabled, non-blocking poll loop, broadcast and per-client send helpers.
- Client: connects to server, listens for messages in a background thread.
- CLI: both binaries use Boost.Program_options for flags.
- CMake ≥ 3.22
- A C++20 compiler with
<format>support- GCC ≥ 13, Clang ≥ 15, AppleClang ≥ 16
- Boost.Program_options
- Ubuntu/Debian:
sudo apt install libboost-program-options-dev
- Ubuntu/Debian:
mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release
Executables are placed under build/bin/ (or CLion's cmake-build-<config>/bin/).
Open two terminals in the project root.
- Start the server
./build/bin/server --port 8080 --max_clients 100
- Start the client
./build/bin/client --host 127.0.0.1 --port 8080
Send data to the server by typing in the client terminal (you can extend the client to send input, currently it receives and prints messages from the server).
-
Server (
server):--port <int>: TCP port to listen on (default: 8080)--max_clients <int>: backlog/clients capacity (default: 100)--help: print help
-
Client (
client):--host <string>: server IPv4 address (default: 127.0.0.1)--port <int>: server port (default: 8080)--help: print help
client.cpp # client binary main
server.cpp # server binary main
pkg/tcp/tcpclient.* # TcpClient implementation
pkg/tcp/tcpserver.* # TcpServer implementation
CMakeLists.txt # CMake build
Makefile # optional/local make (not required by CMake)
.gitignore
- Build fails with
<format>/std::formaterrors:- Ensure compiler version meets requirements (GCC ≥ 13, Clang ≥ 15).
- Link errors for Boost.Program_options:
- Install the dev package (see Requirements) and reconfigure CMake.
- No binaries generated:
- Use the provided CMake steps; executables will be under
build/bin/.
- Use the provided CMake steps; executables will be under
- Port already in use:
- Pick another port via
--portor free the existing one.
- Pick another port via
- The repository
.gitignoreignoresbuild/and CLioncmake-build-*directories. - Signals: the server ignores
SIGPIPEto avoid crashes on broken pipes.