Skip to content

A client/server instant messaging application built in C++17 using POSIX sockets and multithreading.

Notifications You must be signed in to change notification settings

PerretWilliam/messagerie-cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Instant Messaging — C++

C++ Platform

A client/server instant messaging application built in C++17 using POSIX sockets and multithreading.


Table of Contents


Features

Server

  • 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

Client

  • 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

Architecture

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│
└──────────────┘   └──────────────┘   └──────────────┘

Prerequisites

  • Compiler — g++ with C++17 support
  • OS — macOS or Linux (POSIX sockets)
  • Libraries — pthread (included by default)

Installation

Build the project

make

Clean compiled files

make clean

Usage

Start the server

./Build/server [port]
Parameter Description Default
port Server listening port 8080
./Build/server 8080

Start a client

./Build/client <host> <port>
Parameter Description Required
host Server address Yes
port Server port Yes
./Build/client localhost 8080

Client commands

Once 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

Example session

╔═══════════════════════════════════════════════════════════╗
║              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

Communication Protocol

Message structure

struct Message {
    char from[50];      // Sender
    char to[50];        // Recipient ("all" for broadcast)
    char subject[100];  // Subject
    char body[500];     // Content
};

Message delivery

Messages are delivered based on whichever condition is reached first:

Condition Default value
Time interval 5 seconds
Pending message count 5 messages

Project Structure

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

Configuration

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

Technologies

  • Language — C++17
  • Networking — POSIX sockets (TCP/IP)
  • Concurrencystd::thread, std::mutex, std::condition_variable
  • Build — Make / g++

Authors

Perret Williamwilliam-perret.fr

Benjabir Jawadjawad-benjabir.fr

About

A client/server instant messaging application built in C++17 using POSIX sockets and multithreading.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Contributors