Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include

#include #include #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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Process Driven Database Design With Oracle PL SQL

Authors: Rajeev Kaula

1st Edition

1795532386, 978-1795532389

More Books

Students also viewed these Databases questions

Question

5. If yes, then why?

Answered: 1 week ago

Question

6. How would you design your ideal position?

Answered: 1 week ago