Question
BELOW IS THE CODE WRITTEN IN C USING LINUX LIBRARIES(UBUNTU) Convert the multiprocessing TCP/IP into a multithreaded server. This modified server should start a new
BELOW IS THE CODE WRITTEN IN C USING LINUX LIBRARIES(UBUNTU) Convert the multiprocessing TCP/IP into a multithreaded server. This modified server should start a new thread instead of a process every time a new client makes a request to connect. Once connected the thread should communicate with the client NO NEED TO WRITE CLIENT CODE.
#include
#include
void error(const char *msg) { perror(msg); exit(1); }
int main(int argc, char *argv[]) { int sockfd, newsockfd, portno; socklen_t clilen; char c; struct sockaddr_in serv_addr, cli_addr; int n; if (argc < 2) { fprintf(stderr, "ERROR, no port provided "); 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 = portno; if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd, 5); clilen = sizeof(cli_addr); while(1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if(newsockfd < 0) error("ERROR on accept"); do { n = read(newsockfd, &c, 1); if (n < 0) error("ERROR reading from socket"); printf("I got : %c ", c); ++c; n = write(newsockfd, &c, 1); if (n < 0) error("ERROR writing to socket"); } while ( --c != 'Q' ); close(newsockfd); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started