A limitation of the chat server is that it can handle only one client because it...

70.2K

Verified Solution

Question

Programming

A limitation of the chat server is that it can handle only oneclient because it is a single threaded application. Using thepThread library, modify the chat server so that it can handlemultiple clients simultaneously, i.e., by creating a new threadwhenever a client is connected so that the client is handledindividually with a new thread, and at the same time, by having themain thread (i.e., the thread that runs the main function) of thechat server continue to wait for an incoming connection from aclient.

The current implementation of the chat server allows the serverto exchange messages with a client individually. Modify the servercode so that when the server sends a message, that message is sentto all connected clients. We will test your program with threeclients.

Create another thread for the chat server that broadcastscurrent system time to all connected clients every 5 seconds. Forexample, all clients should receive a message containing currentsystem time periodically and print it out.

Server.c

#include

#include

#include

#include

#include

#include

#include

#define MAX 80

#define PORT 8080

#define SA struct sockaddr

#define MAX 80

#define PORT 8080

#define SA struct sockaddr

  

// Function designed for chat between client and server.

void func(int sockfd)

{

    char buff[MAX];

    int n;

    // infinite loop for chat

    for (;;) {

        bzero(buff,MAX);

  

        // read themessage from client and copy it in buffer

        read(sockfd,buff, sizeof(buff));

        // print bufferwhich contains the client contents

        printf(\"Fromclient: %s\t To client : \", buff);

        bzero(buff,MAX);

        n = 0;

        // copy servermessage in the buffer

        while((buff[n++] = getchar()) != '\n')

            ;

  

        // and send thatbuffer to client

        write(sockfd,buff, sizeof(buff));

  

        // if msgcontains \"Exit\" then server exit and chat ended.

        if(strncmp(\"exit\", buff, 4) == 0) {

            printf(\"ServerExit...\n\");

            break;

        }

    }

}

  

// Driver function

int main()

{

    int sockfd, connfd, len;

    struct sockaddr_in servaddr, cli;

  

    // socket create and verification

    sockfd = socket(AF_INET, SOCK_STREAM,0);

    if (sockfd == -1) {

        printf(\"socketcreation failed...\n\");

        exit(0);

    }

    else

        printf(\"Socketsuccessfully created..\n\");

    bzero(&servaddr,sizeof(servaddr));

  

    // assign IP, PORT

    servaddr.sin_family = AF_INET;

    servaddr.sin_addr.s_addr =htonl(INADDR_ANY);

    servaddr.sin_port = htons(PORT);

  

    // Binding newly created socket to givenIP and verification

    if ((bind(sockfd, (SA*)&servaddr,sizeof(servaddr))) != 0) {

        printf(\"socketbind failed...\n\");

        exit(0);

    }

    else

        printf(\"Socketsuccessfully binded..\n\");

  

    // Now server is ready to listen andverification

    if ((listen(sockfd, 5)) != 0) {

        printf(\"Listenfailed...\n\");

        exit(0);

    }

    else

        printf(\"Serverlistening..\n\");

    len = sizeof(cli);

  

    // Accept the data packet from clientand verification

    connfd = accept(sockfd, (SA*)&cli,&len);

    if (connfd < 0) {

        printf(\"serveracccept failed...\n\");

        exit(0);

    }

    else

        printf(\"serveracccept the client...\n\");

  

    // Function for chatting between clientand server

    func(connfd);

  

    // After chatting close the socket

    close(sockfd);

}

Client.c

#include

#include

#include

#include

#include

#define MAX 80

#define PORT 8080

#define SA struct sockaddr

void func(int sockfd)

{

    char buff[MAX];

    int n;

    for (;;) {

        bzero(buff,sizeof(buff));

        printf(\"Enterthe string : \");

        n = 0;

        while((buff[n++] = getchar()) != '\n')

            ;

        write(sockfd,buff, sizeof(buff));

        bzero(buff,sizeof(buff));

        read(sockfd,buff, sizeof(buff));

        printf(\"FromServer : %s\", buff);

        if((strncmp(buff, \"exit\", 4)) == 0) {

            printf(\"ClientExit...\n\");

            break;

        }

    }

}

  

int main()

{

    int sockfd, connfd;

    struct sockaddr_in servaddr, cli;

  

    // socket create and varification

    sockfd = socket(AF_INET, SOCK_STREAM,0);

    if (sockfd == -1) {

        printf(\"socketcreation failed...\n\");

        exit(0);

    }

    else

        printf(\"Socketsuccessfully created..\n\");

    bzero(&servaddr,sizeof(servaddr));

  

    // assign IP, PORT

    servaddr.sin_family = AF_INET;

    servaddr.sin_addr.s_addr =inet_addr(\"127.0.0.1\");

    servaddr.sin_port = htons(PORT);

  

    // connect the client socket to serversocket

    if (connect(sockfd, (SA*)&servaddr,sizeof(servaddr)) != 0) {

        printf(\"connectionwith the server failed...\n\");

        exit(0);

    }

    else

        printf(\"connectedto the server..\n\");

  

    // function for chat

    func(sockfd);

  

    // close the socket

    close(sockfd);

}

Answer & Explanation Solved by verified expert
4.0 Ratings (454 Votes)
Clientc include include include include include include include include include include void doRecievingvoid sockID int clientSocket int sockID while1 char data1024 int read recvclientSocketdata10240 dataread 0 printfsndata int main int clientSocket socketPFINET SOCKSTREAM 0 struct sockaddrin serverAddr serverAddrsinfamily AFINET serverAddrsinport htons8080 serverAddrsinaddrsaddr htonlINADDRANY ifconnectclientSocket struct sockaddr serverAddr sizeofserverAddr 1 return 0 printfConnection established n pthreadt thread pthreadcreatethread NULL doRecieving void clientSocket while1 char input1024 scanfsinput ifstrcmpinputLIST 0 sendclientSocketinput10240 ifstrcmpinputSEND 0 sendclientSocketinput10240 scanfsinput sendclientSocketinput10240 scanfnsinput sendclientSocketinput10240 Serverc include include include include include include include include include include int clientCount 0 static pthreadmutext mutex PTHREADMUTEXINITIALIZER static pthreadcondt cond PTHREADCONDINITIALIZER    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students