Question: #include / / socket ( ) , bind ( ) , listen ( ) , accept ( ) #include / / exit ( ) ,

#include //socket(), bind(), listen(), accept()
#include //exit(), rand(), srand()
#include //send(), close(), read(), write()
#include //htons()
#include //memset()
#include //printf()
#include //time()
#include //pthread_create(), pthread_join()
#define PORT 30601
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
struct client_info {
int fd;
char username[BUFFER_SIZE];
};
void *client_handler(void *arg){
struct client_info *client =(struct client_info *)arg;
char buffer[BUFFER_SIZE];
ssize_t rcount;
// Receive the username from the client
if ((rcount = read(client->fd, client->username, sizeof(client->username)))==-1){
perror("read");
close(client->fd);
pthread_exit(NULL);
}
client->username[rcount]='\0'; // Null terminate the username
printf("New client connected: %s
", client->username);
// Receive messages from client and broadcast
while ((rcount = read(client->fd, buffer, sizeof(buffer)))>0){
// Prepend username to the message
snprintf(buffer + strlen(client->username), BUFFER_SIZE - strlen(client->username),"%s", buffer);
// Broadcast message to other clients
for (int i =0; i < MAX_CLIENTS; ++i){
if (client[i].fd !=-1 && client[i].fd != client->fd){
write(clients[i].fd, buffer, strlen(buffer));
}
}
}
// Close the client socket
close(client->fd);
client->fd =-1; // Mark the client slot as available
printf("Client disconnected: %s
", client->username);
pthread_exit(NULL);
}
struct client_info clients[MAX_CLIENTS]; // Array to store client information
int main(void){
int listen_fd, client_fd, opt, err;
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
pthread_t threads[MAX_CLIENTS];
int thread_count =0;
// Seed random generator
srand(time(NULL));
// Create a new TCP socket
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd ==-1){
perror("socket");
exit(EXIT_FAILURE);
}
// Set socket options to allow quick rebind
opt =1;
if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))==-1){
perror("setsockopt");
exit(EXIT_FAILURE);
}
// Initialize address structure
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = INADDR_ANY;
// Bind socket
if (bind(listen_fd,(struct sockaddr *)&addr, addrlen)==-1){
perror("bind");
exit(EXIT_FAILURE);
}
// Start listening for incoming connections
if (listen(listen_fd, MAX_CLIENTS)==-1){
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d
", PORT);
// Initialize client array
for (int i =0; i < MAX_CLIENTS; ++i){
clients[i].fd =-1; // Mark slot as available
}
// Accept incoming connections and handle them in separate threads
while (1){
// Accept a client connection
client_fd = accept(listen_fd,(struct sockaddr *)&addr, &addrlen);
if (client_fd ==-1){
perror("accept");
continue;
}
// Find an available slot for the client
int slot;
for (slot =0; slot < MAX_CLIENTS; ++slot){
if (clients[slot].fd ==-1){
clients[slot].fd = client_fd;
break;
}
}
if (slot == MAX_CLIENTS){
printf("Maximum number of clients reached. No longer accepting connections.
");
close(client_fd);
continue;
}
// Create a thread to handle the client
if (pthread_create(&threads[thread_count], NULL, client_handler, &clients[slot])!=0){
perror("pthread_create");
close(client_fd);
continue;
}
// Increment thread count
thread_count++;
}
// Join all threads
for (int i =0; i < thread_count; ++i){
pthread_join(threads[i], NULL);
}
// Close the listening socket
close(listen_fd);
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 Finance Questions!