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.
┌─────────────┐ 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
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.bat
Produces build\p2pchat.exe.
make
Produces build/p2pchat.
p2pchat <nickname> <port>
| 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 |
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
[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
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).
Files are sent in 8KB chunks:
FILE_START— filename + sizeFILE_CHUNK× N — binary data chunksFILE_END— signals completion
Received files are saved as received_<filename> in the working directory.