(Solved) : Using C Programming Modify Following Server Client Code Following Serverc Contains Clientc Q42735206 . . .

Using C programming

Modify the following server and client code to do thefollowing:

JIUUUUUU UUIIIIUI LUUIU IIT LIU UUUUUUUUPIUIUUU UIII UIIII UULUI PIU 110 1115. 1. The client and server programs should NOT e

server.c contains

#ifndef SAMPLE_H #define SAMPLE_H #include sample.h #endif static int the_number = 0; void run_server(int server_port) { in

// wait and read the message from client, copy it in buffer int received_size = read(client fd, buffer, sizeof(buffer)); if (

client.c contains

#ifndef SAMPLE_H #define SAMPLE_H #include sample.h #endif void run_client (char *server_addr_str, int server_port) { int s

int main(int argc, char *argv[]) int opt; char *server_addr_str = NULL; unsigned int server_port = 0; while ((opt = getopt(ar

JIUUUUUU UUIIIIUI LUUIU IIT LIU UUUUUUUUPIUIUUU UIII UIIII UULUI PIU 110 1115. 1. The client and server programs should NOT exit after one round of communication. Each client-server connection should loop until the client closes the connection: a. client waits for user’s input and send the input to the server when the user presses enter b. server waits for the connected client’s message and sends the message back to the client via the socket. C. The client, upon receipt of the response from the server, prints out the message and then re-prompts the user for more input. 2. Modify the client program to close the connection with the server when the user types “exit” for the message. If needed, refer to the shell basecode for reference. 3. Modify the server program to do the following when the client connection closes: a. Instead of terminating the server program, loop back to listening for a new client connection. The server should not terminate unless Ctrl-C or SIGKILL is received. (It is a long running program) #ifndef SAMPLE_H #define SAMPLE_H #include “sample.h” #endif static int the_number = 0; void run_server(int server_port) { int sockfd, clientfd, len; struct sockaddr_in servaddr, cli; char buffer[RECV_BUFFER_SIZE]; // socket create and verification sockfd = socket (AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf(“socket creation failed…n”); exit(EXIT_FAILURE); else printf(“Socket successfully createdn”); bzero(&servaddr, sizeof(servaddr)); // assign IP, PORT servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons (server_port); // Binding newly created socket to given IP and verification if ((bind (sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) { printf(“socket bind failedn”); exit(EXIT_FAILURE); else printf(“Socket successfully bindedn”); // Now server is ready to listen and verification if ((listen (sockfd, 1)) != 0) { printf(“Listen failedn”); exit(EXIT_FAILURE); else printf(“Server listening on port: $d.. Waiting for connectionn”, server_port); len = sizeof(cli); // Wait and Accept the connection from client clientfd = accept(sockfd, (SA*)&cli, &len); if (clientfd < 0) { printf(“server acccept failedn”); exit(EXIT_FAILURE); else printf(“Client connetion acceptedn”); bzero(buffer, RECV_BUFFER_SIZE); // wait and read the message from client, copy it in buffer int received_size = read(client fd, buffer, sizeof(buffer)); if (received_size < 0){ printf(“Receiving failedn”); exit(EXIT_FAILURE); // print buffer which contains the client contents printf(“Receive message from client: %s”, buffer); // and send that buffer to client int ret = write(clientfd, buffer, received_size); if (ret < 0){ printf(“Sending failedn”); exit(EXIT_FAILURE); printf(“Send the message back to client: %s”, buffer); // Close the socket at the end close(clientfd); close(sockfd); return; int main(int argc, char* argv[]) int opt; unsigned int port = 0; while ((opt = getopt(argc, argv, “p:n:”)) != -1) { switch (opt) { case ‘p’: port = atoi (optarg); break; case ‘n’: the_number = atoi(optarg); break; default: /* ‘?’ */ fprintf(stderr, “Server Application Usage: % argv[0]); exit(EXIT_FAILURE); -p <port_number>n”, if (port == 0){ fprintf(stderr, “ERROR: Port number for server to listen is not givenn”); fprintf(stderr, “Server Application Usage: % -p <port_number>n”, argv[0]); exit(EXIT_FAILURE); run_server(port); return 0; #ifndef SAMPLE_H #define SAMPLE_H #include “sample.h” #endif void run_client (char *server_addr_str, int server_port) { int sockfd, connfd, len; struct sockaddr_in servaddr, cli; char buffer (MESSAGE_BUFFER_SIZE); // socket create and varification sockfd = socket (AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf(“socket creation failedn”); exit(EXIT_FAILURE); else printf(“Socket successfully createdn”); bzero(&servaddr, sizeof(servaddr)); printf(“Try connecting to server @ %s on port %dn”, server_addr_str, server_port) // assign IP, PORT servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = inet_addr(server_addr_str); servaddr.sin_port = htons (server_port); // connect the client socket to server socket if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) { printf(“connection with the server failed…n”); exit(EXIT_FAILURE); else printf(“connected to the server..n”); sleep(1); bzero(buffer, sizeof(buffer)); printf(“Enter the message that sent to the server: “); int n = 0; while ((buffer[n++] = getchar()) != ‘n’){ write(sockfd, buffer, n); bzero(buffer, sizeof(buffer)); read( sockfd, buffer, sizeof(buffer)); printf(“Message received from Server : %s”, buffer); // close the socket close( sockfd); return; int main(int argc, char *argv[]) int opt; char *server_addr_str = NULL; unsigned int server_port = 0; while ((opt = getopt(argc, argv, “h:P:”)) != -1) { switch (opt) { case ‘h’: server_addr_str = strdup (optarg); break; case ‘p’: server_port = atoi (optarg); break; default: /* ‘?’ */ fprintf(stderr, “Client Application Usage: %s -h <server_ip_or_hostname> -p <server_port_number>n”, argv[0]); exit(EXIT_FAILURE); if (server_addr_str == NULL) { fprintf(stderr, “ERROR: Server’s IP addr or hostname is not givenn”); fprintf(stderr, “Client Application Usage: %s -h <server_ip_or_hostname> -p <server_port_number>n”, argv[0]); exit(EXIT_FAILURE); if (server_port == 0){ fprintf(stderr, “ERROR: Server’s port number for client to connect is not givenn”); fprintf(stderr, “Client Application Usage: %s -h <server_ip_or_hostname> -p <server_port_number>n”, argv[0]); exit(EXIT_FAILURE); run_client(server_addr_str, server_port); return 0; Show transcribed image text JIUUUUUU UUIIIIUI LUUIU IIT LIU UUUUUUUUPIUIUUU UIII UIIII UULUI PIU 110 1115. 1. The client and server programs should NOT exit after one round of communication. Each client-server connection should loop until the client closes the connection: a. client waits for user’s input and send the input to the server when the user presses enter b. server waits for the connected client’s message and sends the message back to the client via the socket. C. The client, upon receipt of the response from the server, prints out the message and then re-prompts the user for more input. 2. Modify the client program to close the connection with the server when the user types “exit” for the message. If needed, refer to the shell basecode for reference. 3. Modify the server program to do the following when the client connection closes: a. Instead of terminating the server program, loop back to listening for a new client connection. The server should not terminate unless Ctrl-C or SIGKILL is received. (It is a long running program)
#ifndef SAMPLE_H #define SAMPLE_H #include “sample.h” #endif static int the_number = 0; void run_server(int server_port) { int sockfd, clientfd, len; struct sockaddr_in servaddr, cli; char buffer[RECV_BUFFER_SIZE]; // socket create and verification sockfd = socket (AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf(“socket creation failed…n”); exit(EXIT_FAILURE); else printf(“Socket successfully createdn”); bzero(&servaddr, sizeof(servaddr)); // assign IP, PORT servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons (server_port); // Binding newly created socket to given IP and verification if ((bind (sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) { printf(“socket bind failedn”); exit(EXIT_FAILURE); else printf(“Socket successfully bindedn”); // Now server is ready to listen and verification if ((listen (sockfd, 1)) != 0) { printf(“Listen failedn”); exit(EXIT_FAILURE); else printf(“Server listening on port: $d.. Waiting for connectionn”, server_port); len = sizeof(cli); // Wait and Accept the connection from client clientfd = accept(sockfd, (SA*)&cli, &len); if (clientfd

Expert Answer


Answer to Using C programming Modify the following server and client code to do the following: server.c contains client.c contains…

Leave a Comment

About

We are the best freelance writing portal. Looking for online writing, editing or proofreading jobs? We have plenty of writing assignments to handle.

Quick Links

Browse Solutions

Place Order

About Us

× How can I help you?