Skip to content

tamoghnodeb/messageapp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

P2P Chat — Decentralized Peer-to-Peer Messaging

A fully modular, cross-platform C++17 peer-to-peer chat application. Each peer acts as both a server and client — no central server required.

Architecture

┌─────────────┐        TCP         ┌─────────────┐
│   Peer A    │◄──────────────────►│   Peer B    │
│  (Server +  │                    │  (Server +  │
│   Client)   │                    │   Client)   │
└─────────────┘                    └─────────────┘
       ▲                                  ▲
       │            TCP                   │
       └──────────────────────────────────┘
                      │
               ┌─────────────┐
               │   Peer C    │
               └─────────────┘

Each peer:

  • Listens on a port for incoming connections
  • Connects outbound to other peers via /connect
  • Runs a dedicated thread per connection for receiving messages
  • Uses a binary wire protocol with magic header, message type, and payload framing

Project Structure

messageapp/
├── include/
│   ├── network/     # Socket abstraction, server, connection
│   ├── protocol/    # Message types, binary serializer
│   ├── core/        # Peer orchestrator, connection manager
│   ├── crypto/      # XOR symmetric encryption
│   └── utils/       # Logger, string/file helpers
├── src/             # Implementations (.cpp files)
├── build.bat        # Windows MSVC build
├── Makefile         # Linux/macOS g++ build
└── README.md

Build

Windows (MSVC Build Tools)

build.bat

Produces build\p2pchat.exe.

Linux / macOS (g++)

make

Produces build/p2pchat.

Usage

p2pchat <nickname> <port>

CLI Commands

Command Description
/connect <ip> <port> Connect to a peer
/disconnect <id> Disconnect from a peer
/msg <id> <message> Send message to specific peer
/sendfile <id> <path> Send a file to a peer
/list List connected peers
/nick <name> Change your nickname
/key <key> Set encryption key (empty to disable)
/help Show commands
/quit Exit
<text> Broadcast message to all peers

Example: 3-Peer Chat

Terminal 1 (Alice):

> p2pchat Alice 9001

Terminal 2 (Bob):

> p2pchat Bob 9002
> /connect 127.0.0.1 9001

Terminal 3 (Charlie):

> p2pchat Charlie 9003
> /connect 127.0.0.1 9001
> /connect 127.0.0.1 9002

Now all three peers can exchange messages:

> Hello everyone!          # Broadcasts to all connected peers
> /msg 1 Hey Alice!        # Private message to peer #1
> /sendfile 2 photo.jpg    # Send file to peer #2
> /key mysecretkey         # Enable XOR encryption
> /list                    # Show connected peers
> /quit                    # Clean exit

Wire Protocol

[4B magic "P2PC"] [1B type] [4B payload_len] [payload]

Payload format: [2B sender_name_len] [sender_name] [data]

Message types: TEXT, FILE_START, FILE_CHUNK, FILE_END, HANDSHAKE, DISCONNECT

Encryption

Set a shared key with /key <passphrase>. Both peers must use the same key. Uses XOR-based symmetric encryption on the payload (header remains unencrypted for framing).

File Transfer

Files are sent in 8KB chunks:

  1. FILE_START — filename + size
  2. FILE_CHUNK × N — binary data chunks
  3. FILE_END — signals completion

Received files are saved as received_<filename> in the working directory.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors