Skip to main content
Be Bitwise
C++Capstone: Chat Server

Chat Server Architecture

For the capstone you're building a multi-client chat server in C++. It pulls together pretty much everything from the track: OOP, RAII, smart pointers, the STL, templates, networking, the lot.

Requirements

The chat server will:

  • Accept multiple TCP client connections
  • Handle JOIN, MSG, LIST, and QUIT commands
  • Broadcast messages to all connected users
  • Track online users by username
  • Use threads for concurrent client handling
  • Clean up resources properly on disconnect

High-Level Design

Here's one MSG travelling through that design, using the handler and broadcast code you'll write over the next two lessons. Notice that bob's handler thread never wakes up: alice's thread writes straight into bob's socket while it holds the SharedState lock.

The two send() calls happen in whatever order the unordered_map iterates. Alice gets her own line back because the MSG handler calls broadcast without an exclude name.

Core Classes

cpp
class ChatServer; // Accepts connections, manages threads
class ClientHandler; // Handles one client's lifecycle
class SharedState; // Thread-safe shared data (user list, broadcast)
struct ChatMessage; // Message data structure

SharedState

The shared state is accessed by all client handler threads:

cpp
class SharedState {
mutable std::mutex mtx;
std::unordered_map<std::string, int> users; // username -> fd
public:
bool add_user(const std::string &name, int fd);
void remove_user(const std::string &name);
std::vector<std::string> list_users() const;
void broadcast(const std::string &message, const std::string &exclude = "");
};
Key Concept

Any method that reaches into shared data has to hold the mutex first. The mutable keyword is the trick that lets const methods like list_users() still acquire the lock, since locking does technically modify the mutex. Standard pattern for thread-safe classes.

ChatServer

cpp
class ChatServer {
Socket listen_socket;
std::shared_ptr<SharedState> state;
public:
ChatServer() : state(std::make_shared<SharedState>()) {}
void start(uint16_t port) {
// bind and listen...
std::cout << "Chat server running on port " << port << std::endl;
while (true) {
int client_fd = accept(listen_socket.get(), nullptr, nullptr);
if (client_fd < 0) continue;
// Each client gets its own thread
// Capture a copy of the shared_ptr, not `this`: a detached
// thread may outlive the ChatServer object
std::thread([state = state, client_fd]() {
ClientHandler handler(client_fd, state);
handler.run();
}).detach();
}
}
};

Why shared_ptr for State?

The SharedState is touched by the main thread and every client-handler thread. shared_ptr keeps it alive as long as anything still holds a reference, so a stray client thread can't outlive the data it's reading. That only works if each thread holds its own copy of the shared_ptr, which is why the lambda captures state by value. Capturing this instead would leave the thread reading this->state through a dangling pointer once the server object is gone.

File Structure

A tidy layout keeps each concern in its own file:

cpp
chat-server/
main.cpp - entry point
server.h/cpp - ChatServer class
client_handler.h/cpp - ClientHandler class
shared_state.h/cpp - SharedState class
message.h - ChatMessage struct
socket.h - Socket RAII wrapper

The next lessons fill in each piece.