Question: Question 4 : Multithreaded Chat Server [ 5 0 points ] In this task, you will take a basic single - threaded chat server and

Question 4: Multithreaded Chat Server [50 points] In this task, you will take a basic single-threaded chat server and modify it to handle multiple clients concurrently using multithreading. The server should allow multiple clients to connect, send messages, and broadcast those messages to all other connected clients. Additionally, you will implement a daemonized version of the multithreaded chat server, allowing it to run in the background. Task Description You are provided with a basic single-threaded chat server (chat_server1.c) that can accept connections and communicate with one client at a time. Your tasks are as follows: Task 1: Multithreaded Chat Server (chat_server2.c)---- Convert the server into a multithreaded application using pthreads to handle multiple client connections concurrently. Implement message broadcasting so that when one client sends a message, it is broadcast to all other connected clients. Ensure thread safety. Use mutexes or condition variables to ensure shared resources (such as the list of connected clients) are accessed safely. Ensure that clients can disconnect gracefully without affecting the rest of the server or losing messages. Task 2: Daemonized Multithreaded Chat Server (chat_server3.c) After successfully implementing the multithreaded chat server (chat_server2.c), your next task is to create a new version of the server that runs as a daemon. This version should operate in the background and continue to run even if the terminal session is closed. It should accept multiple client connections and handle them just like the multithreaded version.
chat_server1.c:
#include
#include
#include
#include
#include
#include
#define PORT 8080
#define BUFFER_SIZE 1024
void handle_client(int client_socket){
char buffer[BUFFER_SIZE];
int bytes_read;
while ((bytes_read = read(client_socket, buffer, BUFFER_SIZE -1))>0){
buffer[bytes_read]='\0'; // Null-terminate the message
printf("Client: %s
", buffer); // Print the message received from the client
// Echo the message back to the client
write(client_socket, buffer, strlen(buffer));
// If the client sends "exit", break the loop and close the connection
if (strncmp(buffer, "exit", 4)==0){
printf("Client disconnected.
");
break;
}
}
close(client_socket); // Close the client socket when done
}
int main(){
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
// Create the server socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket <0){
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Set up the server address
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
// Bind the socket to the address
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr))<0){
perror("Bind failed");
close(server_socket);
exit(EXIT_FAILURE);
}
// Start listening for client connections
if (listen(server_socket, 10)<0){
perror("Listen failed");
close(server_socket);
exit(EXIT_FAILURE);
}
printf("Server started on port %d. Waiting for clients to connect...
", PORT);
// Accept a single client connection (single-threaded)
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_len);
if (client_socket <0){
perror("Client accept failed");
close(server_socket);
exit(EXIT_FAILURE);
}
printf("Client connected!
");
// Handle communication with the connected client
handle_client(client_socket);
// Close the server socket
close(server_socket);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!