A client/server instant messaging application built in C++17 using POSIX sockets and multithreading.
- Features
- Architecture
- Prerequisites
- Installation
- Usage
- Communication Protocol
- Project Structure
- Configuration
- Multi-client — Handles multiple simultaneous client connections
- Message queue — Messages delivered in batches or after a configurable delay
- Broadcast — Send messages to all connected users at once
- Logging — Full history of connections, disconnections, and messages
- Thread-safe — Robust synchronization with mutexes and condition variables
- Real-time messaging — Asynchronous message reception
- Inbox — Track read and unread messages
- User list — View currently connected users
- History — Browse the delivered message log
- Colored terminal UI — Color-coded output for better readability
The application uses a multithreaded client/server architecture:
┌─────────────────────────────────────────────────────────────────┐
│ SERVER │
├─────────────────────────────────────────────────────────────────┤
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Main │ │ Delivery │ │ Handler │ │
│ │ Thread │ │ Thread │ │ Thread │ ... │
│ │ (accept) │ │ (sending) │ │ (client) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │
│ └────────────────┴────────────────┘ │
│ │ │
│ ┌───────────┴───────────┐ │
│ │ Shared Data │ │
│ │ - Pending messages │ │
│ │ - Connected users │ │
│ │ - Message history │ │
│ └───────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
│
TCP/IP Sockets
│
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ CLIENT 1 │ │ CLIENT 2 │ │ CLIENT N │
├──────────────┤ ├──────────────┤ ├──────────────┤
│ Main Thread │ │ Main Thread │ │ Main Thread │
│ Listen Thread│ │ Listen Thread│ │ Listen Thread│
└──────────────┘ └──────────────┘ └──────────────┘
- Compiler — g++ with C++17 support
- OS — macOS or Linux (POSIX sockets)
- Libraries — pthread (included by default)
makemake clean./Build/server [port]| Parameter | Description | Default |
|---|---|---|
port |
Server listening port | 8080 |
./Build/server 8080./Build/client <host> <port>| Parameter | Description | Required |
|---|---|---|
host |
Server address | Yes |
port |
Server port | Yes |
./Build/client localhost 8080Once connected, the following commands are available:
| Command | Description |
|---|---|
LIST |
Display unread messages |
USERS |
List connected users |
COMPOSE |
Write and send a message |
LOG |
Browse message history |
QUIT |
Disconnect from the server |
╔═══════════════════════════════════════════════════════════╗
║ WELCOME TO INSTANT MESSAGING ║
╚═══════════════════════════════════════════════════════════╝
Connecting to localhost:8080...
Connection successful!
Enter your username: Alice
Hello Alice! You are now connected.
Available commands:
LIST - View your messages
USERS - See connected users
COMPOSE - Send a message
LOG - View message history
QUIT - Exit
> COMPOSE
Recipient (or 'all' for broadcast): Bob
Subject: Hey!
Message: How are you?
Message sent!
> USERS
Connected users:
- Alice (you)
- Bob
struct Message {
char from[50]; // Sender
char to[50]; // Recipient ("all" for broadcast)
char subject[100]; // Subject
char body[500]; // Content
};Messages are delivered based on whichever condition is reached first:
| Condition | Default value |
|---|---|
| Time interval | 5 seconds |
| Pending message count | 5 messages |
messagerie-cpp/
├── Build/ # Compiled executables
│ ├── server
│ └── client
├── src/
│ ├── client.cpp # Client entry point
│ ├── server.cpp # Server entry point
│ ├── messaging_client.cpp # Client logic
│ ├── messaging_client.hpp
│ ├── messaging_server.cpp # Server logic
│ ├── messaging_server.hpp
│ ├── socket_wrapper.cpp # POSIX socket wrapper
│ ├── socket_wrapper.hpp
│ ├── logger.cpp # Logging system
│ ├── logger.hpp
│ └── message.hpp # Message struct and constants
├── Makefile
└── README.md
Main constants are defined in src/message.hpp:
#define MAX_FROM_LENGTH 50 // Max sender field length
#define MAX_TO_LENGTH 50 // Max recipient field length
#define MAX_SUBJECT_LENGTH 100 // Max subject length
#define MAX_BODY_LENGTH 500 // Max message body length
#define DEFAULT_PORT 8080 // Default port
#define DELIVERY_INTERVAL 5 // Delivery interval (seconds)
#define DELIVERY_BATCH_SIZE 5 // Batch size before delivery- Language — C++17
- Networking — POSIX sockets (TCP/IP)
- Concurrency —
std::thread,std::mutex,std::condition_variable - Build — Make / g++
Perret William — william-perret.fr
Benjabir Jawad — jawad-benjabir.fr