Skip to content

listnt/TcpServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TCP Server/Client (C++20)

Minimal TCP server and client written in modern C++20 using POSIX sockets and Boost.Program_options.

Features

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

Requirements

  • 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

Build (CMake)

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/).

Run

Open two terminals in the project root.

  1. Start the server
./build/bin/server --port 8080 --max_clients 100
  1. 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).

Command-line options

  • 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

Project structure

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

Troubleshooting

  • Build fails with <format>/std::format errors:
    • 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/.
  • Port already in use:
    • Pick another port via --port or free the existing one.

Notes

  • The repository .gitignore ignores build/ and CLion cmake-build-* directories.
  • Signals: the server ignores SIGPIPE to avoid crashes on broken pipes.

About

minimal implementation of tcp server/cllient

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published