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);
}