A high-performance, multi-threaded chat server implementation in C++ with user authentication, admin controls, and testing.
- Multi-threaded message processing with worker threads
- Connection pooling with automatic cleanup of stale connections
- Message queuing with size limits
- Persistent chat history with SQLite storage
- Private messaging between users
- Secure user authentication with salted password hashing
- Admin controls for user management
- Real-time server statistics and metrics
- Active user listing and session management
- Comprehensive unit testing with Google Test
- Stress testing capabilities for performance validation
- Non-blocking socket operations
- Thread-safe operations with mutex protection
- Automatic connection timeout handling
- Message size validation and limits
- C++17 or higher
- CMake 3.14 or higher
- SQLite3
- OpenSSL (for secure password hashing)
- Google Test (automatically fetched during build)
- pthread library
# Install required packages (Ubuntu/Debian)
sudo apt-get update
sudo apt-get install build-essential cmake libsqlite3-dev libssl-dev# Clone the repository with submodules
git clone https://github.com/jjcyz/client-server-chat.git
cd client-server-chat
git submodule init
git submodule update
# Build
mkdir -p build
cd build
cmake ..
make
# The executables will be created in build# Run server (in one terminal)
./server
# Run client (in another terminal)
./clientThe server will start listening on port 5555 by default.
A Bloomberg Terminal-style web frontend is available for the chat server.
# Install dependencies for WebSocket bridge
cd websocket-bridge
pnpm install
# Install dependencies for frontend
cd ../frontend
pnpm installYou need to run three services:
- C++ Chat Server (Terminal 1):
cd build
./server- WebSocket Bridge Server (Terminal 2):
cd websocket-bridge
pnpm start- Frontend Development Server (Terminal 3):
cd frontend
pnpm devThe frontend will be available at http://localhost:3000
- Bloomberg Terminal aesthetic (dark theme, terminal styling)
- Real-time chat interface
- User authentication (login/register)
- Active users sidebar
- Server statistics panel
- Support for all chat commands
- Private messaging
See frontend/README.md for more details.
- Registration/Login required: Users must register or log in before chatting
- Password Security:
- Passwords are salted and hashed using SHA-256 via OpenSSL
- Salt is randomly generated for each user
- Hashed passwords are stored in SQLite database
- Session Management:
- Unique session IDs for each login
- Session timeout handling
- Automatic cleanup of expired sessions
- Admin users: Admins can remove users from the system using a special command
/register <username> <password>— Register a new user/login <username> <password>— Log in as an existing user/stats— Show server statistics (messages, connections, uptime)/list— List active users and their status/msg <username> <message>— Send private message to/removeuser <username>— (Admin only) Remove a user from the system
- Max connections: 200
- Message queue size: 2000
- Worker threads: 4
- Chat history size: 1000 messages
- Default port: 5555
- Buffer size: 1024 bytes
- Connection timeout: 60 seconds
- Connection pooling for efficient resource management
- Non-blocking socket operations
- Multi-threaded message processing
- Thread-safe operations with mutex protection
- Automatic cleanup of stale connections
- Message size validation and limits
- SQLite3 (
chat_server.db) - Tables:
- Users (id, username, password_hash, salt, created_at, is_admin)
- Messages (id, sender_id, receiver_id, content, created_at)
receiver_id = 0indicates broadcast messagesreceiver_id > 0indicates private messages
- Hybrid storage approach:
- In-memory cache (
chat_history) for fast access to recent messages - Database persistence for long-term message storage
- Recent messages loaded from database on server startup
- In-memory cache (
- Unit tests for server and client
- Mock server implementation for client testing
- Stress testing capabilities
- Concurrent operation testing
- Connection failure testing
- Message size limit testing
- C++ Standard: C++17
- Build System: CMake
- Testing Framework: Google Test
- IDE Support: VSCode configuration included
- CI/CD: GitHub Actions workflow
# Clean build
cd build
rm -rf *
cmake ..
make
# Build only
make
# Run tests
make test
# Run server and client
./build/server # Terminal 1
./build/client # Terminal 2# Method 1: Using CMake
cd build
make test
# Method 2: Using the test script
./run_tests.shNote: User and admin data is persistent in chat_server.db. You can inspect or modify it using standard SQLite tools. The database is automatically created on first run if it doesn't exist.