Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. This program sends two integers to the server, and the server returns the sum of these two integers. 2. Use this structure: struct data

1. This program sends two integers to the server, and the server returns the sum of these two integers.

2. Use this structure:

struct data {

int arg1;

int arg2;

int result; };

3. Write two procedures that are used to transfer this structure. These procedures are :int sendData(int sock, int arg1, int arg2); int receiveData(int sock, int &result);The first procedure sends the data over the socket that is passed as a parameter and the second procedure receives data over the socket. Both return 0 on success and -1 if an error occurs.

Note that receiveData() returns the value read through its second parameter. Both of these procedures must use the structure described above to send the data. Rewrite the client and server programs to use these procedures to send the data over the network.

/******************************************************** * client.c * A more robust client example. In this example the * client has two arguments, integers. These integers * are sent to the server, which returns the sum of the * two integers. *******************************************************/

#include #include #include #include #include #include #include #include #include "lib.h"

int main(int argc, char **argv) { struct addrinfo hints; struct addrinfo *addr; struct sockaddr_in *addrinfo; int rc; int sock; char buffer[512]; int len; int arg1, arg2; int result; int ret;

if(argc != 3) { printf("Usage: client num1 num2 "); exit(1); }

arg1 = atoi(argv[1]); arg2 = atoi(argv[2]);

/* * clear the hints structure to zero */ memset(&hints, 0, sizeof(hints));

/* * want a stream on a compatible interface */ hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_ADDRCONFIG; /* * localhost is the name of the current computer */ rc = getaddrinfo("localhost", NULL, &hints, &addr); if(rc != 0) { printf("Host name lookup failed: %s ", gai_strerror(rc)); exit(1); }

/* * use the first result from getaddrinfo */ addrinfo = (struct sockaddr_in *) addr->ai_addr;

sock = socket(addrinfo->sin_family, addr->ai_socktype, addr->ai_protocol); if(sock < 0) { printf("Can't create socket "); exit(1); }

/* * specify the port number */ addrinfo->sin_port = htons(4321);

rc = connect(sock, (struct sockaddr *) addrinfo, addr->ai_addrlen); if(rc != 0) { printf("Can't connect to server "); exit(1); }

/* * free the results returned by get addrinfo */ freeaddrinfo(addr);

/* * send a message to the server and echo the response */ ret = writen(sock, (char*) &arg1, sizeof(arg1)); if(ret != sizeof(arg1)) { printf("Error sending first argument "); close(sock); exit(1); } ret = writen(sock, (char*) &arg2, sizeof(arg2)); if(ret != sizeof(arg2)) { printf("Error sending second argument "); close(sock); exit(1); } ret = readn(sock, (char*) &result, sizeof(result)); if(ret != sizeof(result)) { printf("Error reading result "); close(sock); exit(1); } printf("The sum of %d and %d is %d ", arg1, arg2, result); ret = writeString(sock, "Thank You"); if(ret) { printf("Error sending thank you "); close(sock); exit(1); }

close(sock);

exit(0); }

/*********************************************** * server.c * A more robust server that receives two * integers from a client and returns their * sum. In return it gets a nice message * from the client. **********************************************/

#include #include #include #include #include #include #include #include #include "lib.h"

int main(int argc, char **argv) { int sock, conn; int i; int rc; struct sockaddr address; socklen_t addrLength = sizeof(address); struct addrinfo hints; struct addrinfo *addr; int len; int arg1, arg2; int ret; char *message;

/* * set the hints structure to zero */ memset(&hints, 0, sizeof(hints));

/* * want a stream, also address that will accept all connections on this host */ hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; if((rc = getaddrinfo(NULL, "4321", &hints, &addr))) { printf("host name lookup failed: %s ", gai_strerror(rc)); exit(1); }

/* * use the first entry returned by getaddrinfo */ sock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); if(sock < 0) { printf("Can't create socket "); exit(1); }

/* * want to be able to reuse the address right after the socket is closed. Otherwise must wait for 2 minutes */ i = 1; setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));

rc = bind(sock, addr->ai_addr, addr->ai_addrlen); if(rc < 0) { printf("Can't bind socket "); exit(1); }

/* * free results returned by getaddrinfo */ freeaddrinfo(addr);

rc = listen(sock, 5); if(rc < 0) { printf("Listen failed "); exit(1); }

/* * accept an arbitrary number of connections in a loop */ while((conn = accept(sock, (struct sockaddr*) &address, &addrLength)) >= 0) { /* * read message from client and respond */ ret = readn(conn, (char*) &arg1, sizeof(arg1)); if(ret != sizeof(arg1)) { printf("Error reading arg1 "); close(conn); continue; } ret = readn(conn, (char*) &arg2, sizeof(arg2)); if(ret != sizeof(arg2)) { printf("Error reading arg2 "); close(conn); continue; } printf("Data from client: %d %d ", arg1, arg2); arg1 = arg1+arg2; ret = writen(conn, (char*) &arg1, sizeof(arg1)); if(ret != sizeof(arg1)) { printf("Error sending result "); close(conn); continue; } message = readString(conn); if(message != NULL) { printf("Message from client: %s ", message); free(message); } else { printf("Error receiving message from client "); } close(conn); }

close(sock); exit(0); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions