Skip to content

potsju/Multiclient-TCP-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TCP Chat Application

A multi-client chat server and client written in C++ using POSIX sockets, supporting kqueue(linux epoll). Features real-time messaging, file transfer, private messaging, and two server architectures.

C++ Platform License

Features

  • Multi-client support - Multiple users can connect simultaneously
  • Real-time messaging - Instant message delivery to all connected clients
  • Private messaging - Send direct messages to specific users
  • File transfer - Send files to individual users or broadcast to all
  • User presence - See who's online
  • Colored output - Easy-to-read terminal interface with timestamps
  • Two server architectures - Thread-per-client and event-driven (kqueue)

Server Architectures

This project includes two server implementations to demonstrate different approaches:

1. Thread-per-Client (main.cpp)

┌─────────┐ ┌─────────┐ ┌─────────┐
│ Thread 1│ │ Thread 2│ │ Thread 3│
│ Client 1│ │ Client 2│ │ Client 3│
└─────────┘ └─────────┘ └─────────┘
  • Simple to understand
  • One thread per connected client
  • Best for < 1,000 concurrent users
  • Uses mutex for thread safety

2. Event-Driven (server_kqueue.cpp)

┌─────────────────────────────────┐
│         SINGLE THREAD           │
│  kevent() monitors ALL sockets  │
└─────────────────────────────────┘
  • High performance
  • Single thread handles all clients
  • Scales to 10,000+ concurrent users
  • Uses macOS kqueue (similar to Linux epoll)

Requirements

  • C++17 or later
  • POSIX-compliant OS (macOS, Linux)
  • pthread library

Building

# Clone the repository
git clone https://github.com/yourusername/tcp-chat.git
cd tcp-chat

# Compile thread-per-client server
g++ -std=c++17 -o server main.cpp -pthread

# Compile event-driven server (macOS)
g++ -std=c++17 -o server_kqueue server_kqueue.cpp -pthread

# Compile client
g++ -std=c++17 -o client client.cpp -pthread

Usage

Starting a Server

# Option 1: Thread-per-client server
./server

# Option 2: Event-driven server (recommended for high load)
./server_kqueue

Connecting as a Client

./client

You'll be prompted to enter a username, then you can start chatting.

Client Commands

Command Description
/help Show all available commands
/quit Disconnect and exit
/clear Clear the terminal screen
/users List all online users
/msg <user> <message> Send a private message
/sendfile <user> <filepath> Send a file to a user
/sendfile all <filepath> Send a file to everyone
/me <action> Send an action message

Server Commands

Command Description
/help Show all available commands
/quit Shutdown the server
/list List connected clients
/stats Show server statistics (kqueue version)
<message> Broadcast message to all clients

Example Session

Terminal 1 (Server):

$ ./server_kqueue

[System] Server started on port 54000
[System] Mode: Event-driven (kqueue)
[System] 'alice' joined the chat!
[System] 'bob' joined the chat!
[14:30:01] alice: Hello everyone!
[14:30:05] bob: Hey alice!

Terminal 2 (Alice):

$ ./client
Enter your username: alice
[System] Connected!

Hello everyone!
[14:30:01] You: Hello everyone!
[14:30:05] bob: Hey alice!
/msg bob secret message
[PM to bob] secret message

Terminal 3 (Bob):

$ ./client
Enter your username: bob
[System] Connected!

[14:30:01] alice: Hello everyone!
Hey alice!
[14:30:05] You: Hey alice!
[PM from alice]: secret message

File Transfer

Files are saved to a downloads/ folder in the current directory.

# Send to specific user
/sendfile bob photo.png

# Send to everyone
/sendfile all document.pdf

Project Structure

tcp-chat/
├── main.cpp           # Thread-per-client server
├── server_kqueue.cpp  # Event-driven server (kqueue)
├── client.cpp         # Chat client
├── README.md          # This file
├── .gitignore         # Git ignore file
└── downloads/         # Received files (created automatically)

Technical Details

Protocol

Messages use a simple text-based protocol with prefixes:

Prefix Format Purpose
FILE: FILE:recipient:filename:size:data File transfer
MSG: MSG:recipient:message Private message
USERS USERS Request user list

Thread Safety (Thread-per-client server)

  • std::mutex protects the shared clients vector
  • std::atomic<bool> for clean shutdown signaling
  • lock_guard for RAII-style locking

Event-Driven Architecture (kqueue server)

  • Non-blocking sockets with O_NONBLOCK
  • kqueue() creates kernel event queue
  • kevent() monitors multiple file descriptors
  • Single thread handles all I/O operations

Socket Options

  • SO_REUSEADDR enabled for quick server restarts

Architecture Comparison

Aspect Thread-per-Client Event-Driven (kqueue)
Threads 1 per client 1 total
Memory Higher (thread stacks) Lower
Complexity Simpler More complex
Scalability ~1,000 clients 10,000+ clients
Mutex needed Yes No

About

A multi-client chat server and client written in C++ using POSIX sockets. Features real-time messaging, file transfer, and private messaging.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages