This project implements a multi-threaded TCP server in C++ using the Winsock API. The server supports multiple client connections concurrently using a thread pool to manage tasks. Each client communicates with the server and receives a key and initialization vector (IV) for encryption purposes. Messages sent by one client are broadcast to all other connected clients.
- Thread Pooling: The server uses a custom thread pool to handle multiple client connections concurrently, optimizing resource management.
- Non-blocking I/O: The server socket is non-blocking, using
select()to wait for activity. - Message Broadcasting: The server broadcasts any received message from one client to all other connected clients.
- Dynamic Key Generation: On each connection, the server generates and sends unique encryption keys
- AES Encryption: The client includes AES encryption functionality (defined in AES.h and AES.cpp), ready for future use to secure communication.
├── client
│ ├── src
│ │ ├── main.cpp # Client-side source code
│ │ ├── AES.h # AES encryption header
│ │ ├── AES.cpp # AES encryption implementation
│ ├── X64
│ │ └── Debug
│ │ └── client.exe # Compiled client executable
├── server
│ ├── src
│ │ ├── main.cpp # Server-side source code
│ │ ├── Threadpool.h # Threadpool header for handling multiple connections
│ ├── X64
│ │ └── Debug
│ │ └── server.exe # Compiled server executable
└── README.md # Project documentation
-
main.cpp:
- Initializes the server.
- Generates random encryption keys and IVs to be sent to the clients.
- Manages incoming client connections and messages.
- Uses a thread pool to handle each client connection without blocking the main thread.
- Implements the main loop for receiving messages and broadcasting them to other clients.
-
Threadpool.h:
- Implements a thread pool that allows for the efficient execution of tasks (in this case, handling client connections).
- Uses a queue to manage tasks and assigns them to available threads.
- Threads are reused for handling new clients and perform tasks concurrently.
- The server generates a random key and IV using the
generate_random_hex_stringfunction. These values are sent to clients for future encryption purposes.
- Each client connects to the server through a TCP socket.
- The server sends the key and IV to each client upon connection.
- Clients can send messages to the server, which are then broadcast to all other connected clients (except the sender).
- The
Threadpoolclass is responsible for managing a fixed number of worker threads. - Tasks (such as handling individual client sockets) are added to a queue and processed by the next available thread.
- Socket Programming: TCP-based server-client communication using Winsock.
- Multithreading: Efficient client handling using a thread pool to avoid creating/destroying threads repeatedly.
- Concurrency Control: Use of
mutexandcondition_variableto safely manage shared resources in the thread pool. - Non-blocking I/O: Using
select()to handle multiple sockets without blocking the main thread. - Random Key Generation: Generates random hex strings for encryption keys and IVs.
You can run the project using either Visual Studio 2022 or by executing the precompiled binaries located in the X64/Debug directories for both the server and client.
- Visual Studio 2022 (with C++ development environment setup)
- Windows operating system (the project relies on Winsock2 for socket communication)
-
Open the Project: Open the solution files for both the server and client in Visual Studio 2022.
-
Build the Project: Build both the server and client by selecting
Build>Build Solutionin Visual Studio. -
Run the Server:
- Start the server by pressing
F5orCtrl+F5from the Visual Studio environment. - The server listens for client connections on port
8080.
- Start the server by pressing
-
Run the Client:
- Once the server is running, open another instance of Visual Studio and run the client in the same way (
F5orCtrl+F5). - The client will connect to the server and you can begin exchanging messages.
- Once the server is running, open another instance of Visual Studio and run the client in the same way (
If you do not want to use Visual Studio 2022, you can directly run the compiled executables available in the X64/Debug folders for both the client and server.
- Navigate to the
server/X64/Debugfolder. - Run the
server.exefile.
The server will start listening for client connections on port 8080.
- Navigate to the
client/X64/Debugfolder. - Run the
client.exefile. The client will attempt to connect to the server atlocalhost(127.0.0.1) on port8080.
- The server listens for incoming client connections.
- Once a client connects, the server:
- Sends encryption keys (
keyandivvalues). - Uses the thread pool to handle communication between clients.
- Receives and broadcasts messages to all other connected clients.
-
The client establishes a connection to the server.
-
It receives encryption keys from the server.
-
The client can send messages to the server, which will then broadcast them to all other clients.
-
AES Encryption: The client has AES encryption methods implemented in
AES.handAES.cpp. While encryption is currently not applied to transmitted messages, these files lay the foundation for securing communication in the future.
- Winsock2: The project uses the Winsock2 library for networking on Windows. Ensure your environment supports Winsock2 for proper functionality.
To include the necessary Winsock library in Visual Studio, the following directive is used:
#pragma comment(lib, "ws2_32.lib")
---- Add GUI support for both client and server using libraries like Qt or WinForms.
- Support for more advanced features like file sharing, error handling, and more robust encryption.