Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

T1. Modify Client.c program to accept two arguments (IP add & port no. of the co

ID: 3866545 • Letter: T

Question

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 <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <pthread.h>

#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] = '';
printf("Message: %s ",messageBuf);
if (write(PARMPTR->connectionDesc,"GOT IT",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 <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>


#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 <Server IP> <Server Port>");
exit(1);
}
  
//Create a socket for the client
//If sockfd<0 there was an error in the creation of the socket
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
perror("Problem in creating the socket");
exit(2);
}
  
//Creation of the socket
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr= inet_addr(argv[1]);
// servaddr.sin_port = htons(argv[2]);
servaddr.sin_port = htons(SERV_PORT); //convert to big-endian order
  
//Connection of the client to the socket
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
perror("Problem in connecting to the server");
exit(3);
}
  
while (fgets(sendline, MAXLINE, stdin) != NULL) {
  
send(sockfd, sendline, strlen(sendline), 0);
      
if (recv(sockfd, recvline, MAXLINE,0) == 0){
//error: server terminated prematurely
perror("The server terminated prematurely");
exit(4);
}
printf("%s", "String received from the server: ");
fputs(recvline, stdout);
}

exit(0);
}

Explanation / Answer

Server.c

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <pthread.h>
#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] = '';
printf("Message: %s ",messageBuf);
if (write(PARMPTR->connectionDesc,"GOT IT",7) < 0)
       {
perror("Server: write error");
return(0);
}
}
close(PARMPTR->connectionDesc); /* Avoid descriptor leaks */
free(PARMPTR); /* And memory leaks */
return(0); /* Exit thread */
}
int main (int argc, char const *argv[])
{
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 sin_port *) &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, (struct sockaddr *)&address, (socklen_t*)&addrlen));       //Accept The Arguements of Client.c
/* 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 <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#define MAXLINE 4096 /*max text line length*/
intmain(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
char sendline[MAXLINE], recvline[MAXLINE];
// alarm(300); // to terminate after 300 second
//basic check of the arguments
//additional checks can be inserted
if (argc !=2)
{
perror("Usage: TCPClient <Server IP> <Server Port>");
exit(1);
}
//Create a socket for the client
//If sockfd<0 there was an error in the creation of the socket
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
perror("Problem in creating the socket");
exit(2);
}
//Creation of the socket
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr= inet_addr(argv[1]);   //Pass 1st Arguement as IPAddress.
servaddr.sin_port = htons(argv[2]);        //Pass 2nd Arguement as Port Number.
//Connection of the client to the socket
if (connect(sockfd, (struct sockport *) &servaddr, sizeof(servaddr))<0)
{
perror("Problem in connecting to the server");
exit(3);
}
while (fgets(sendline, MAXLINE, stdin) != NULL)
{   
    send(sockfd, sendline, strlen(sendline), 0);
   if (recv(sockfd, recvline, MAXLINE,0) == 0)
   {
        //error: server terminated prematurely
       perror("The server terminated prematurely");
   exit(4);
   }
   printf("%s", "String received from the server: ");
   fputs(recvline, stdout);
}
exit(0);
}