Multi-threaded TCP Server in C++
A fully functional multi-threaded TCP server built from scratch using modern C++, without using any networking frameworks or external libraries.
This server:
- Accepts multiple clients concurrently
- Uses a dedicated worker thread per client
- Uses a consumer thread and a thread-safe priority queue
- Supports acknowledgement responses back to clients
- Demonstrates real-world concurrency, socket programming, and synchronization
๐ Features
socket(), bind(), listen(), accept(), recv(), send() implemented manually
- Worker-consumer architecture using:
std::thread
std::mutex & std::lock_guard
std::condition_variable
std::priority_queue
- Message object with timestamp & priority
- Safe shared resource handling using RAII and smart pointers
๐ Build Instructions
Compile
g++ main.cpp -o server -pthread
Run Server
Connect Client (macOS / Linux)
Then type messages (press Enter to send).
๐ Project Architecture
Server Flow Diagram
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ SERVER START โ
โ 1. Create socket โ
โ 2. Bind to port 9090 โ
โ 3. Listen for connections โ
โ 4. Start consumer thread โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ACCEPT LOOP (Main Thread) โ
โ - Waits for client connections โ
โ - Creates unique client ID โ
โ - Stores socket in map โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโ
โ โ
โผ โผ
โโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโ
โ WORKER THREAD โ โ WORKER THREAD โ
โ (Client 1) โ โ (Client 2) โ
โ โโโโโโโโโโโโโโโโโโโ โ โ โโโโโโโโโโโโโโโโโโโ โ
โ โ 1. recv() data โ โ โ โ 1. recv() data โ โ
โ โ 2. Create Messageโ โ โ โ 2. Create Messageโ โ
โ โ - clientId โ โ โ โ - clientId โ โ
โ โ - timestamp โ โ โ โ - timestamp โ โ
โ โ - text โ โ โ โ - text โ โ
โ โ - priority โ โ โ โ - priority โ โ
โ โ 3. Push to queue โ โ โ โ 3. Push to queue โ โ
โ โโโโโโโโโโโโโโโโโโโ โ โ โโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโ โโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THREAD-SAFE PRIORITY QUEUE โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Protected by mutex โ โ
โ โ Sorted by: โ โ
โ โ 1. Priority (high โ low) โ โ
โ โ 2. Timestamp (old โ new) โ โ
โ โ Condition variable notifies โ โ
โ โ consumer when msg arrives โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CONSUMER THREAD โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ 1. pop() from queue (blocking) โ โ
โ โ 2. Log message to console โ โ
โ โ 3. Find client socket โ โ
โ โ 4. send() ACK to client โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโ
โ CLIENT โ
โ Receives ACKโ
โโโโโโโโโโโโโโโ
Detailed Message Flow
โโโโโโโโโโโ โโโโโโโโโโโ
โ CLIENT โ โ SERVER โ
โโโโโโฌโโโโโ โโโโโโฌโโโโโ
โ โ
โ 1. TCP Connection (nc localhost 9090) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโถ
โ โ
โ 2. accept() creates worker thread
โ clientId = "client-0" โ
โ Store in clientSocketMap โ
โ โ
โ 3. Type message: "Hello Server" โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโถ
โ โ
โ 4. Worker Thread recv() โโโโโ โ
โ Creates Message object โ โ
โ - clientId: "client-0" โ โ
โ - timestamp: 1234567890 โ โ
โ - text: "Hello Server" โ โ
โ - priority: 1 โ โ
โ โ โ
โ 5. queue.push(msg) โโโโโโโโโโค โ
โ notify_one() โ โ
โ โ โ
โ 6. Consumer Thread wakes up โ โ
โ queue.pop() โ โ
โ Prints: [client-0][...] โ โ
โ โ โ
โ 7. Lookup clientSocketMap โ โ
โ Find socket for client-0 โ โ
โ โ โ
โ 8. ACK: "Received: Hello Server" โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ send() โโโโโโโโโโโโโโโโโโโ โ
โ โ
โ 9. Client sees response โ
โ โ
Component Breakdown
Main Thread
โโ Accept Loop โโโฌโโถ Worker Thread 1 โโโ
โโโถ Worker Thread 2 โโโค
โโโถ Worker Thread 3 โโโผโโถ Priority Queue โโโถ Consumer Thread
โโโถ Worker Thread 4 โโโค
โโโถ Worker Thread N โโโ
๐ง What I Learned
This project was a complete learning experience from the ground up. I gained deep understanding of:
- TCP networking fundamentals and how servers work internally
- Threading concepts - how threads are created, synchronized, and managed
- The entire lifecycle of a server: socket creation, binding, listening, accepting connections
- Internal phases of request handling: receive โ queue โ process โ acknowledge
- Synchronization, race conditions, and thread safety mechanisms
- Message prioritization concepts and queue management
- Real backend system design patterns and architecture
I built this entirely from scratch while learning with ChatGPT guidance. It helped me understand not just "how to code it" but "how it actually works" - from the low-level socket operations to high-level threading patterns. Every line of code taught me something new about systems programming and concurrent server architecture.
โจ Future Enhancements
- Thread pool instead of thread-per-client
- Priority parsing (
/urgent message)
- Graceful shutdown
- Broadcast chat
- Logging to file
๐ค Acknowledgements
Special thanks to ChatGPT for guiding conceptual understanding and architectural direction while I implemented all logic manually.
๐ License
MIT License