PLEASE USE MULTITHREADING IN THIS PROGRAM SO THAT MANY CLIENTS CONNECTED TO THE SERVER AND EACH CLIENT CAN SEND MESSAGE TO SERVER AND SERVER MESSAGE SHOWN TO ALL CLIENTS AND ONE CLIENT MESSAGE SHOWN TO ALL CLIENTS.PLEASE MAKE CHANGINGS IN THIS CODE. SERVER #include #include #include #include #include #include #include #include #include #define PORT 5500 void func(int connfd){    char buffer[1024];    int n;    while(1) {        bzero(buffer, 1024);        read(connfd, buffer, sizeof(buffer));        printf("From client: %s\t To client : ", buffer);        bzero(buffer, 1024);        n = 0;        while ((buffer[n++] = getchar()) != '\n');        write(connfd, buffer, sizeof(buffer));        if (strncmp("exit", buffer, 4) == 0){        printf("Server Exit...\n");        break;    }       } } int main(){    int sockfd, connfd, len;    struct sockaddr_in serverAddr, cli;    sockfd = socket(AF_INET, SOCK_STREAM, 0);    if (sockfd == -1) {        printf("socket creation failed...\n");        exit(0);    }    else{        printf("Socket successfully created..\n");    }    memset(&serverAddr, '\0', sizeof(serverAddr));    serverAddr.sin_family = AF_INET;    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");    serverAddr.sin_port = htons(PORT);    if((bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr))) != 0) {        printf("socket bind failed...\n");        exit(0);    }    else{        printf("Socket successfully binded..\n");    }    if((listen(sockfd, 5)) != 0) {        printf("Listen failed...\n");        exit(0);    }    else{        printf("Server listening..\n");    }    len = sizeof(cli);    connfd = accept(sockfd, (struct sockaddr*)&cli, &len);    if (connfd < 0) {        printf("server accept failed...\n");        exit(0);    }    else{        printf("server accept the client...\n");    }    func(connfd);    close(sockfd); } CLIENT #include #include #include #include #include #include #include #include #include #define PORT 5500 int main(){    int clientSocket, ret;    struct sockaddr_in serverAddr;    char buffer[1024];    clientSocket = socket(AF_INET, SOCK_STREAM, 0);    if(clientSocket < 0){        printf("[-]Error in connection.\n");        exit(1);    }    printf("[+]Client Socket is created.\n");    memset(&serverAddr, '\0', sizeof(serverAddr));    serverAddr.sin_family = AF_INET;    serverAddr.sin_port = htons(PORT);    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");    ret = connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));    if(ret < 0){        printf("[-]Error in connection.\n");        exit(1);    }    printf("[+]Connected to Server.\n");    while(1){        printf("Client: \t");        scanf("%s", &buffer[0]);        send(clientSocket, buffer, strlen(buffer), 0);        if(strcmp(buffer, ":exit") == 0){            close(clientSocket);            printf("[-]Disconnected from server.\n");            exit(1);        }        if(recv(clientSocket, buffer, 1024, 0) < 0){            printf("[-]Error in receiving data.\n");        }else{            printf("Server: \t%s\n", buffer);        }    }    return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

PLEASE USE MULTITHREADING IN THIS PROGRAM SO THAT MANY CLIENTS CONNECTED TO THE SERVER AND EACH CLIENT CAN SEND MESSAGE TO SERVER AND SERVER MESSAGE SHOWN TO ALL CLIENTS AND ONE CLIENT MESSAGE SHOWN TO ALL CLIENTS.PLEASE MAKE CHANGINGS IN THIS CODE.

SERVER

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#define PORT 5500

void func(int connfd){
   char buffer[1024];
   int n;
   while(1) {
       bzero(buffer, 1024);
       read(connfd, buffer, sizeof(buffer));
       printf("From client: %s\t To client : ", buffer);
       bzero(buffer, 1024);
       n = 0;
       while ((buffer[n++] = getchar()) != '\n');
       write(connfd, buffer, sizeof(buffer));
       if (strncmp("exit", buffer, 4) == 0){
       printf("Server Exit...\n");
       break;
   }
  
   }
}
int main(){
   int sockfd, connfd, len;
   struct sockaddr_in serverAddr, cli;
   sockfd = socket(AF_INET, SOCK_STREAM, 0);
   if (sockfd == -1) {
       printf("socket creation failed...\n");
       exit(0);
   }
   else{
       printf("Socket successfully created..\n");
   }
   memset(&serverAddr, '\0', sizeof(serverAddr));
   serverAddr.sin_family = AF_INET;
   serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
   serverAddr.sin_port = htons(PORT);
   if((bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr))) != 0) {
       printf("socket bind failed...\n");
       exit(0);
   }
   else{
       printf("Socket successfully binded..\n");
   }
   if((listen(sockfd, 5)) != 0) {
       printf("Listen failed...\n");
       exit(0);
   }
   else{
       printf("Server listening..\n");
   }
   len = sizeof(cli);
   connfd = accept(sockfd, (struct sockaddr*)&cli, &len);
   if (connfd < 0) {
       printf("server accept failed...\n");
       exit(0);
   }
   else{
       printf("server accept the client...\n");
   }
   func(connfd);
   close(sockfd);
}

CLIENT

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#define PORT 5500

int main(){
   int clientSocket, ret;
   struct sockaddr_in serverAddr;
   char buffer[1024];

   clientSocket = socket(AF_INET, SOCK_STREAM, 0);
   if(clientSocket < 0){
       printf("[-]Error in connection.\n");
       exit(1);
   }
   printf("[+]Client Socket is created.\n");

   memset(&serverAddr, '\0', sizeof(serverAddr));
   serverAddr.sin_family = AF_INET;
   serverAddr.sin_port = htons(PORT);
   serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

   ret = connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
   if(ret < 0){
       printf("[-]Error in connection.\n");
       exit(1);
   }
   printf("[+]Connected to Server.\n");

   while(1){
       printf("Client: \t");
       scanf("%s", &buffer[0]);
       send(clientSocket, buffer, strlen(buffer), 0);

       if(strcmp(buffer, ":exit") == 0){
           close(clientSocket);
           printf("[-]Disconnected from server.\n");
           exit(1);
       }

       if(recv(clientSocket, buffer, 1024, 0) < 0){
           printf("[-]Error in receiving data.\n");
       }else{
           printf("Server: \t%s\n", buffer);
       }
   }

   return 0;
}

Expert Solution
steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education