Question

IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

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 <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\n",
PARMPTR->connectionDesc);
if (PARMPTR->connectionDesc < 0) {
printf("Accept failed\n");
return(0); /* Exit thread */
}
  
/* Receive messages from sender... */
while ((recievedMsgLen=
read(PARMPTR->connectionDesc,messageBuf,sizeof(messageBuf)-1)) > 0)
{
recievedMsgLen[messageBuf] = '\0';
printf("Message: %s\n",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\n");
}

}

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

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\n",
PARMPTR->connectionDesc);
if (PARMPTR->connectionDesc < 0)
   {
printf("Accept failed\n");
return(0); /* Exit thread */
}
/* Receive messages from sender... */
while ((recievedMsgLen=read(PARMPTR->connectionDesc,messageBuf,sizeof(messageBuf)-1)) > 0)
{
recievedMsgLen[messageBuf] = '\0';
printf("Message: %s\n",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 */
}
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\n");
}
}

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

Add a comment
Know the answer?
Add Answer to:
IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will...

    For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will modifiy echoServer.c and echoClient.c programs (for the server's port# not fixed). (1) Modify echoServer.c program to take one argument (a port number) to be used for its listening port when it starts. (2) Modify echoClient.c program to take two arguments (server's IP address and Port number) to be used for its connection. (3) Find a port free for the server using netstat (see...

  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...

    Run the code in Linux and provide the screenshot of the output and input #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> static void cleanup(); static void docleanup(int signum); static const char *SERVER_ADDR = "127.0.0.1"; static const int SERVER_PORT = 61234; static int cfd = -1; int main(int argc, char *argv[]) { struct sockaddr_in saddr; char buf[128]; int bufsize = 128, bytesread; struct sigaction sigact; printf("client starts running ...\n"); atexit(cleanup); sigact.sa_handler =...

  • C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in...

    C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in the comments. * */ #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <time.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/uio.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <strings.h> #define BUFFERT 512 #define BACKLOG 1 int create_server_socket (int port); struct sockaddr_in sock_serv,sock_clt; int main(int argc, char** argv){ int sfd, fd; unsigned int length = sizeof(struct sockaddr_in); long int n, m, count...

  • /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from...

    /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from various connections * in real time. * * Student: */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <sys/socket.h> #include <sys/un.h> #include <stdlib.h> #include <pthread.h> #include "message-lib.h" // forward declarations int usage( char name[] ); // a function to be executed by each thread void * recv_log_msgs( void * arg ); // globals int log_fd; // opened by main() but accessible...

  • Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically...

    Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically what each line actually does? whats the function? whats the point? Don't tell me what the program does as a whole, I need to understand what each line does in this program. #include #include #include #include #include #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) {    FILE *fp;    struct hostent *hp;    struct sockaddr_in sin;    char *host;...

  • In this activity, you will complete the RSS Client that connects to a UNL RSS feed,...

    In this activity, you will complete the RSS Client that connects to a UNL RSS feed, processes the XML data and outputs the results to the standard output. Most of the client has been completed for you. You just Lab Handout: Structures 4 need to complete the design and implementation of a C structure that models the essential parts of an RSS item. Your structure will need to support an RSS item’s title, link, description, and publication date. As a...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • I need help fixing this code in C. It keeps giving me an error saying error:...

    I need help fixing this code in C. It keeps giving me an error saying error: ‘O_RDWD’ undeclared (first use in this function) if ((fd = open(fifo, O_RDWD)) < 0) note: each undeclared identifier is reported only once for each function it appears in. Please fix so it compiles properly. //************************************************************************************************************************************************** #include <fcntl.h> #include <stdio.h> #include <errno.h> #include<stdlib.h> #include <string.h> #include<unistd.h> #include <sys/stat.h> #include <sys/types.h> #define MSGSIZE 63 char *fifo = "fifo"; int main(int argc, char **argv){    int fd;...

  • I am supposed to write documentation and report for the code below but I am new...

    I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT