Question: 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

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 (which is available). ** For example, to find a port (between 30000 and 30999) which is available, with netstat command.

netstat -aont | grep "`hostnname -i`:30[0-9][0-9][0-9] "

Note: the back-tick (backward single quotation mark to run the command on the run).

------------------------------------------------------------------------------------------------------

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); }>

exit(0); }

-----------------------------------------------------------------------------------------------------

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 "); }

}

---------------------------------------------------------------------------------------------------------------

// codes to get IP and Port# from arguments.

servaddr.sin_addr.s_addr = inet_addr(argv[1]);

servaddr.sin_port = htons((int) strtol(argv[2], (char **)NULL, 10));

// Note.

----------------------------------

inet_aton: converts the C character string pointed to by strptr into its 32-bit binary network byte ordered value, which is stored through the pointer addrptr

inet_addr: does the same conversion, returning the 32-bit binary network byte ordered value as the return value. It is deprecated and any new code should use inet_aton instead

inet_ntoa: converts a 32-bit binary network byte ordered IPv4 address into its corresponding dotted-decimal string.

-------------------------------------------------------------------------------------------------------------------------

Sample code for concurrent EchoServer with process

--------------------------------------------------

// concurrent echoServer with process - a child process to serve a client

int main(int argc, char **argv)

{

int listenfd, connfd;

socklen_t clientlen;

struct sockaddr_storage clientaddr;

Signal(SIGCHLD, sigchld_handler);

listenfd = Open_listenfd(argv[1]);

while (1) {

clientlen = sizeof(struct sockaddr_storage);

connfd = Accept(listenfd, (SA *) &clientaddr, &clientlen);

if (Fork() == 0) {

Close(listenfd); /* Child closes its listening socket */

echo(connfd); /* Child services client */

Close(connfd); /* Child closes connection with client */

exit(0); /* Child exits */

}

Close(connfd); /* Parent closes connected socket (important!) */

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!