Question: 1) Modify the attached client and server socket files to allow both the client and the server to send an unlimited amount of messages back

1) Modify the attached client and server socket files to allow both the client and the server to send an unlimited amount of messages back and forth

2) The connection can only be closed when the client sends the message "Bye!" to the server

3)Please document your code

4) Submit your code and a detailed README file, which should explain how to run the code with sample input and output

CLIENT CODE:

#include #include #include #include #include //Defines the structure hostent

void error(char *msg) //Same error function as in server { perror(msg); exit(0); }

int main(int argc, char *argv[]) { int sockfd, portno, n; struct sockaddr_in serv_addr; //The address of the server that client wants to connect to struct hostent *server; //Defines the variable server as a pointer to a structure of type hostent char buffer[256]; if (argc < 3) { fprintf(stderr,"usage %s hostname port ", argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname(argv[1]); //Client attempts to get the hostent structure for the server if (server == NULL) { fprintf(stderr,"ERROR, no such host "); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); //Initialize serv_addr serv_addr.sin_family = AF_INET; //Set the fields in serv_addr bcopy((char *)server->h_addr, //void bcopy(char *s1, char *s2, int length). server->h_addr is a character string, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) //Connect to server. function returns 0 on success and ?1 on failure error("ERROR connecting"); printf("Please enter the message: "); //Prompt user for message after connection is successful bzero(buffer,256); //Initialize buffer fgets(buffer,255,stdin); //Read from stdin into buffer n = write(sockfd,buffer,strlen(buffer)); //Write buffer into socket. Returns number of characters written if (n < 0) //Check for writing errors error("ERROR writing to socket"); bzero(buffer,256); n = read(sockfd,buffer,255); //Reads servers response into buffer if (n < 0) error("ERROR reading from socket"); printf("%s ",buffer); //Prints servers response to screen return 0; //Exit }

SERVER CODE:

#include  //Declarations used in most input and output operations; #include  //Defines a number of data types used in system calls #include  //Defines a number of structures needed for sockets; #include  //Contains constants and structures needed for Internet domain addresses. void error(char *msg) // Displays an error message on stderr and then aborts the program { perror(msg); exit(1); } int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen; /*sockfd and newsockfd, are array subscripts into the file descriptor table. They store the values returned by the socket system call and the accept system call.portno stores the port number on which the server accepts connections. clilen stores the size of the address of the client, which is needed for the accept system call. */ char buffer[256]; //The server reads characters from the socket connection into the buffer char. struct sockaddr_in serv_addr, cli_addr; //client and server address structures, using the sockaddr_ in Internet address structure. This structure is defined in netinet/in.h. int n; //The number of characters read or written by the read() and write() calls if (argc < 2) { //check that the user has provided a port number argument and displays an error message fprintf(stderr,"ERROR, no port provided "); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); //Create new streaming IPV4 socket. 0 indicates default protocol, which is TCP. Returns file descriptor table entry if (sockfd < 0) //Checks for errors in the creation of the socket. A negative file descriptor table usually indicates an error. error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); //Set all values in a buffer to zero, bzero(buf_addr,buf_size) portno = atoi(argv[1]); //Retrieves the port no provided as a string and converts it to an integer serv_addr.sin_family = AF_INET; //Assign values to the variable serv_addr, which is a structure of type struct sockaddr_in serv_addr.sin_port = htons(portno); //Converts a port number in host byte order to a port number in network byte order. serv_addr.sin_addr.s_addr = INADDR_ANY; //IPv4 address of the server, which is obtained from the symbolic constant INADDR_ANY. if (bind(sockfd, (struct sockaddr *) &serv_addr, //Bind operation and error checking. Second parameter is cast into right type sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(sockfd,5); //Socket listens for new connections. 2nd argument is the number of connections that can be waiting while the process is handling a particular connection clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); //Causes the process to block until a new client request comes in if (newsockfd < 0) error("ERROR on accept"); bzero(buffer,256); //Initialize buffer n = read(newsockfd,buffer,255); //Read up to 255 bytes into buffer. Returns no. of xters read. Blocks until client writes if (n < 0) error("ERROR reading from socket"); //Check for errors while reading printf("Here is the message: %s ",buffer); //Print message to stdout n = write(newsockfd,"I got your message",18); //Acknowledge the message if (n < 0) error("ERROR writing to socket"); //Checks for errors in writing return 0; //Terminates } 

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!