A multi-threaded TCP sockets chat server that supports multiple concurrent clients.
This project implements a client-server architecture with the following features:
- Multi-threaded design: Both client and server use separate threads for handling message sending, receiving, and processing
- Non-blocking I/O: Socket operations are non-blocking for better performance
- Message queue mechanism: Uses a thread-safe message queue for handling incoming messages
- Connection management: Handles multiple client connections simultaneously using TCP sockets.
The server component receives connections from multiple clients, processes their messages, and broadcasts them to all connected clients. Key features:
- Accepts and manages multiple client connections
- Thread-safe client list management
- Processes and broadcasts messages
- Coordinates shutdown when all clients have finished sending messages
The client component is a fuzzer (mainly for testing the server) which connects to the server, sends a specified number of random messages, and receives messages from all clients.
Key features:
- Simultaneous message sending and receiving using threads
- Logs received messages to a specified file
- Generates random content for test messages
- Terminates gracefully after sending specified number of messages
Messages have a simple format:
- Type byte: 0 for regular messages, 1 for termination signals
- Sender information: For type 0 messages, contains sender IP and port
- Message content: The actual message data
- Terminator: Newline character (\n)
./server <port number> <# of clients>
Parameters:
- port number: The port on which the server will listen for connections
- # of clients: The number of expected client connections
./client <IP address> <port number> <# of messages> <log file path>
Parameters:
- IP address: IP address of the server
- port number: Port number the server is listening on
- # of messages: Number of messages to send before terminating
- log file path: Path to file where received messages will be logged
Compile the project using the following commands:
# Compile server
gcc -o server server.c -pthread
# Compile client
gcc -o client client.c -pthread
-
Start the server (on port 8080, expecting 3 clients):
./server 8080 3 -
Start 3 clients (each sending 10,15,20 messages, writing received messages to their own log files) :
./client 127.0.0.1 8080 10 client1.log
./client 127.0.0.1 8080 15 client2.log
./client 127.0.0.1 8080 20 client3.log
- Each client will send its specified number of messages, then signal completion by sending a termination message
- When all clients have signaled completion, the server will send termination messages to all clients
- All processes will exit gracefully
- Written in C using POSIX threads and socket APIs
- Uses TCP sockets for reliable, ordered message delivery
- Uses mutex locks and condition variables for thread synchronization
- Implements efficient buffer management for message processing
- Uses non-blocking I/O for better performance and responsiveness
Criterion is used for unit tests. Server's basic features are tested (setting socket to non-blocking, adding clients, enqueueing messages, message handling, etc)