Linux Multi-threaded Client-Server Using Shared Memory
This project demonstrates a multi-threaded client-server architecture in Linux, using shared memory for communication between the server and its clients. The server can handle multiple clients simultaneously, logging all interactions to a file.
Features
- Multi-threaded Server: The server can handle multiple clients concurrently.
- Shared Memory: Communication between the server and clients is facilitated through shared memory.
- Semaphores: Synchronization is achieved using semaphores to ensure data integrity in the shared memory.
- Logging: All interactions are logged to a file named shared_memory_log.txt.
Getting Started
Prerequisites
- A Linux-based system
- GCC compiler
- POSIX thread library
- POSIX semaphore library
Compilation To compile the server and client programs, use the following commands:
gcc -pthread -o server server.c -lrt gcc -o client client.c -lrt
Running the Server Start the server using:
./server The server will listen on port 4567 for incoming client connections.
Running the Client Start a client using:
./client You can run multiple clients to test the multi-threaded functionality of the server.
Project Structure
- server.c: The server code, which handles incoming client connections, updates shared memory, and logs interactions.
- client.c: The client code, which connects to the server, sends messages, and reads responses.
- shared_memory_log.txt: The log file where all interactions are recorded.
How It Works
Server
- Initializes shared memory and semaphores.
- Listens for incoming client connections.
- For each client, creates a new thread to handle communication.
- Updates shared memory with client messages and server responses. 5.Logs all interactions to shared_memory_log.txt.
Client
- Connects to the server.
- Sends messages to the server.
- Receives responses from the server.
- Reads and displays the contents of the shared memory.
Example Usage
- Start the server:
./server
- Start a client:
./client
- Enter messages in the client terminal. The server will respond, and the interactions will be logged and displayed.
Notes
Ensure that the server is running before starting any clients. To stop the server, use Ctrl+C. To exit a client, type exit and press Enter.
Author [Meena G]