Question

want code in C:

and please answer the question!Goal: To improve the client-server model introduced in the class Requirements: The server should be able to accept requests from multiple clients and communicate with them. .Each client should be able to communicate multiple rounds of messages with the server. .Whenever the server receives a message from a client, it replies with I got your message To implement the server with multithreading. .A client program should terminate its communication with the server when its user types in EXIT, causing no effect on other clients You can manually terminate the server using Ctrl C or you can implement it to terminate itself on a certain input from keyboard .The server and client should take arguments in the following formats server.exe [port number] client.exe [servers name] [port number] Your programs will be tested and graded using computers in Lab QBB 244 under the LINUX/UNIX mode. To submit .Source codes

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please let me know if you have any doubts or you want me to modify the code. And if you find this code useful then don't forget to rate my answer as thumps up. Thank you! :)


#include <stdio.h>
#include <stdlib.h>              
#include <string.h>
#include <unistd.h>
#include <sys/types.h>          
#include <sys/socket.h>          
#include <netinet/in.h>          
#include <pthread.h>      

void error(const char *msg)  
{
    perror(msg);
    exit(1);
}

void *threadFunction(int mysockfd)
{
   char bufferThread[256];
   int nThread;
  
   while(1)
   {
       bzero(bufferThread,256);
       nThread = read(mysockfd,bufferThread,255);
       //read message from mysockfd
      
       if (nThread < 0) error("ERROR reading from socket");
       printf("Here is the message: %s\n",bufferThread);
      
       if(strcmp(bufferThread, "EXIT\n") == 0)
       {
           printf("Network Terminated\n");
           close(mysockfd);
           return NULL;
       }
       //close mysockfd if message is "EXIT\n"
          
       nThread = write(mysockfd,"I got your message",18);
       //acknowledge to client
      
       if (nThread < 0) error("ERROR writing to socket");
   }
  
   close(mysockfd);
   return NULL;
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;

     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }

     sockfd = socket(AF_INET, SOCK_STREAM, 0);

     if (sockfd < 0)
        error("ERROR opening socket");

     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);


     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0)
              error("ERROR on binding");
  
   while(1)
   {

       listen(sockfd,5);

       clilen = sizeof(cli_addr);
       newsockfd = accept(sockfd,
                   (struct sockaddr *) &cli_addr,
                   &clilen);
       if (newsockfd < 0)
       error("ERROR on accept");
  
       pthread_t pth;   //this is the thread identifier
       pthread_create(&pth,NULL,threadFunction,newsockfd);
       //generage a new thread
      
   }
   close(sockfd);
     return 0;
}

Add a comment
Know the answer?
Add Answer to:
want code in C: and please answer the question! Goal: To improve the client-server model introduced...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT