Question: IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent
IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING:
T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c).
Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to.
Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is the minute.
//ConServerThread.c
#include
#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 "); }
}
// Client.c
#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(0); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
