Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C language, linux Try this exercise by modifying server and client - see task1 and task2. Task1. Modify Client.c program to accept two arguments (IP

C language, linux

Try this exercise by modifying server and client - see task1 and task2.

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

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

Task3. 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.

[Note: To pick a port # that no one uses. If you have a unique port# of your favorite (for example, my favorite port# 10300 from my UTD netid which is rkm010300) and for sure no one will ever use it, then you may use this port# throughout this exercise. Or we may try to get a port number (without using someone's and to use a port# which is available between 10K and 60K) using current date and time. For example, if current time is: November 10, 2016. 14:22 then the port number is (hhmm6): 14226. Once you get this port# to use, then you need to make sure that this port number is not taken by someone else to check it by using netstat command: "netstat -aont | grep 14226".]

Client.c Program

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

#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

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

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

7. It is advisable to do favors for people whenever possible.

Answered: 1 week ago