Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Unix 1 - Modify Client.c program to accept two arguments (IP address and port number of the concurrent Server with thread - conServThread.c). 2 -

Unix

1 - Modify Client.c program to accept two arguments (IP address and port number of the concurrent Server with thread - conServThread.c).

2 - Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to.

3 - Try these two updated programs (server and client) with a port (which is available).

Client.c

#include  #include  #include  #include  #include  #include  #include  #define MAXLINE 4096 /*max text line length*/ #define SERV_PORT 10010 /*port*/ int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr; char sendline[MAXLINE], recvline[MAXLINE]; // alarm(300); // to terminate after 300 seconds //basic check of the arguments //additional checks can be inserted if (argc !=2) { perror("Usage: TCPClient  "); exit(1); } //Create a socket for the client //If sockfd<0 there was an error in the creation of socket if ((sockfd =socket (af_inet, sock_stream, 0)) <0) { perror("problem creating socket"); exit(2); }> 

conServerThread.c

#include  #include  #include  #include  #include  /* CONCURRENT SERVER: THREAD EXAMPLE Must be linked with the "pthread" library also, e.g.: cc -o example example.c -lnsl -lsocket -lpthread This program creates a connection socket, binds a name to it, then listens for connections to the sockect. When a connection is made, it accepts messages from the socket until eof, and then waits for another connection... This is an example of a CONCURRENT server -- by creating threads several clients can be served at the same time... This program has to be killed to terminate, or alternately it will abort in 120 seconds on an alarm... */ #define PORTNUMBER 10010 struct serverParm { int connectionDesc; }; void *serverThread(void *parmPtr) { #define PARMPTR ((struct serverParm *) parmPtr) int recievedMsgLen; char messageBuf[1025]; /* Server thread code to deal with message processing */ printf("DEBUG: connection made, connectionDesc=%d ", PARMPTR->connectionDesc); if (PARMPTR->connectionDesc < 0) { printf("Accept failed "); return(0); /* Exit thread */ } /* Receive messages from sender... */ while ((recievedMsgLen= read(PARMPTR->connectionDesc,messageBuf,sizeof(messageBuf)-1)) > 0) { recievedMsgLen[messageBuf] = '\0'; printf("Message: %s ",messageBuf); if (write(PARMPTR->connectionDesc,"GOT IT\0",7) < 0) { perror("Server: write error"); return(0); } } close(PARMPTR->connectionDesc); /* Avoid descriptor leaks */ free(PARMPTR); /* And memory leaks */ return(0); /* Exit thread */ } main () { int listenDesc; struct sockaddr_in myAddr; struct serverParm *parmPtr; int connectionDesc; pthread_t threadID; /* For testing purposes, make sure process will terminate eventually */ alarm(120); /* Terminate in 120 seconds */ /* Create socket from which to read */ if ((listenDesc = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("open error on socket"); exit(1); } /* Create "name" of socket */ myAddr.sin_family = AF_INET; myAddr.sin_addr.s_addr = INADDR_ANY; myAddr.sin_port = htons(PORTNUMBER); if (bind(listenDesc, (struct sockaddr *) &myAddr, sizeof(myAddr)) < 0) { perror("bind error"); exit(1); } /* Start accepting connections.... */ /* Up to 5 requests for connections can be queued... */ listen(listenDesc,5); while (1) /* Do forever */ { /* Wait for a client connection */ connectionDesc = accept(listenDesc, NULL, NULL); /* Create a thread to actually handle this client */ parmPtr = (struct serverParm *)malloc(sizeof(struct serverParm)); parmPtr->connectionDesc = connectionDesc; if (pthread_create(&threadID, NULL, serverThread, (void *)parmPtr) != 0) { perror("Thread create error"); close(connectionDesc); close(listenDesc); exit(1); } printf("Parent ready for another connection "); } }

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_2

Step: 3

blur-text-image_3

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

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions

Question

7. Senior management supports the career system.

Answered: 1 week ago