A high-performance, asynchronous chat room application built with C++ and Boost.Asio. This project demonstrates modern C++ networking patterns, allowing multiple clients to connect to a server and exchange messages in real-time.
- Asynchronous I/O: Uses Boost.Asio for non-blocking network operations, enabling the server to handle multiple clients concurrently without blocking
- Real-time Messaging: Instant message delivery to all connected clients
- Scalable Architecture: Supports up to 100 concurrent participants per room
- Thread-safe Design: Proper use of shared pointers and async patterns for safe concurrent access
- Message Protocol: Custom message encoding with header-based length detection
- Clean Code: Well-documented codebase with camelCase naming conventions
- C++20 compatible compiler (GCC 10+ or Clang 12+)
- Boost Libraries (version 1.70 or higher)
libboost-system-devlibboost-thread-dev
- Make build system
- Linux/Unix environment (tested on Ubuntu)
Ubuntu/Debian:
sudo apt-get update
sudo apt-get install libboost-system-dev libboost-thread-devmacOS (using Homebrew):
brew install boostFedora/RHEL:
sudo dnf install boost-develThe application follows a client-server architecture with the following key components:
- Handles message encoding and decoding
- 4-byte header stores message body length
- Maximum message size: 512 bytes
- Provides methods for header encoding/decoding and body extraction
- Getter methods are
constfor better code safety and const correctness
- Abstract base class for all chat room participants
- Defines interface for message delivery and writing
- Manages all connected participants
- Handles joining/leaving participants
- Broadcasts messages to all participants except the sender
- Supports up to 100 concurrent participants
- Represents a single client connection
- Handles asynchronous read/write operations
- Manages message queue for each client
- Implements the Participant interface
- Accepts incoming client connections
- Creates Session instances for each client
- Manages the chat room lifecycle
- Connects to the server
- Handles user input in a separate thread
- Asynchronously receives and displays messages from the server
-
Client sends message:
- User types message → Client thread captures input
- Message posted to I/O context → Written to socket
-
Server receives message:
- Session reads from socket → Creates Message object
- Message delivered to Room → Room broadcasts to all participants
-
Server sends to clients:
- Room calls
write()on each Session (except sender) - Session queues message → Asynchronously writes to client socket
- Room calls
-
Client receives message:
- Client reads from socket → Displays message to user
git clone <repository-url>
cd chatRoomCpp-mainmakeThis will create two executables:
chatApp- The chat serverclientApp- The chat client
make clean./chatApp <port>Example:
./chatApp 9099The server will start listening on the specified port and wait for client connections.
In a separate terminal, run:
./clientApp <port>Example:
./clientApp 9099- Start the server on a port (e.g.,
9099) - Connect multiple clients to the same port
- Type messages in any client terminal
- Messages will be broadcast to all other connected clients
- Press
Ctrl+Cto disconnect
Example Session:
# Terminal 1 - Server
$ ./chatApp 9099
Received: Hello everyone!
# Terminal 2 - Client 1
$ ./clientApp 9099
Enter message: Hello everyone!
Server: Hi there!
# Terminal 3 - Client 2
$ ./clientApp 9099
Server: Hello everyone!
Enter message: Hi there!chatRoomCpp-main/
├── chatRoom.hpp # Header file with Room, Session, and Participant classes
├── chatRoom.cpp # Server implementation and Room/Session logic
├── client.cpp # Client application implementation
├── message.hpp # Message class with encoding/decoding
├── Makefile # Build configuration
├── README.md # This file
├── .github/
│ └── workflows/
│ └── c-cpp.yml # GitHub Actions CI/CD workflow
└── old/ # Original files (for reference)
├── chatRoom.hpp
├── chatRoom.cpp
├── client.cpp
├── message.hpp
└── Makefile
Messages are structured as follows:
- Header (4 bytes): ASCII-encoded decimal number representing body length (e.g., "0050" for 50 bytes)
- Body (up to 512 bytes): The actual message content
Example:
Header: "0025" (25 bytes)
Body: "Hello, this is a message!"
Total: 29 bytes
Server:
- Single I/O thread running
io_context.run() - All async operations handled by the I/O context
- No explicit threading needed (Boost.Asio handles concurrency)
Client:
- Main thread: Runs I/O context for async operations
- Input thread: Captures user input and posts writes to I/O context
- Thread-safe communication via
boost::asio::post()
- Port Validation: Port numbers are validated to ensure they are in the valid range (1-65535)
- Input Validation: Uses
std::stoifor robust port number parsing with proper exception handling - Connection errors are caught and logged
- Invalid messages are rejected (header validation)
- Client disconnections are handled gracefully
- Server continues running after client disconnects
- Specific error messages for invalid port numbers and out-of-range values
-
Start the server:
./chatApp 9099
-
Connect multiple clients in separate terminals:
./clientApp 9099
-
Send messages from different clients and verify they appear in all other clients
The project includes a GitHub Actions workflow (.github/workflows/c-cpp.yml) that:
- Builds the project on every push and pull request
- Installs required dependencies
- Verifies successful compilation
- Naming Convention: camelCase for variables and functions
- Comments: Comprehensive documentation for classes and methods
- Standards: C++20 with
-Wall -Wextrawarnings enabled - Const Correctness: Getter methods are marked as
constfor better code safety - Error Handling: Uses modern C++ exception handling (
std::stoi,std::invalid_argument,std::out_of_range)
- RAII: Automatic resource management with smart pointers
- Async/Await Pattern: Boost.Asio async operations with completion handlers
- Observer Pattern: Room notifies all participants of new messages
- Factory Pattern: Server creates Session instances for new connections
- Maximum message size: 512 bytes
- Maximum participants per room: 100
- No authentication or user identification
- No message history persistence
- No private messaging (all messages are broadcast)
- User authentication and identification
- Private messaging between users
- Message history and persistence
- Multiple chat rooms
- File transfer support
- Encrypted communication
- WebSocket support for web clients
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is open source and available under the MIT License.
- Built with Boost.Asio for asynchronous networking
- Inspired by modern C++ networking patterns
For questions or suggestions, please open an issue on GitHub.
Happy Chatting! 💬