Question: Your job is to modify both the client and server applications as such: Server. The server should function as an echo server, which means it

Your job is to modify both the client and server applications as such:
Server. The server should function as an echo server, which means it simply returns to the client exactly the message it receives from the client. In the server code, a call to read() on the socket descriptor copies the received message into a buffer. This modification requires that message be transmitted back to the client with a call to write() on the same socket descriptor. In the server code, include a printf() statement to standard output indicating the message is being transmitted to the client.
Client. After transmitting the message to the server, the client process should then await the reply. To do this, in the client code add a call to read() on the same socket descriptor used to send the message. By default, read() is a blocking system call, so the client process will wait for a response from the server before it continues execution. Once the reply is received, output a statement to standard output indicating the message was received (and be sure include the message in the output). Add code to ADD YOUR CODE HERE
------------server.c--------------------
#include
#include
#include
#include
#include
#include
#include
#include
#define DEFAULT_PORT 4433
#define BUFFER_SIZE 256
int create_socket(unsigned int port){
int s;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
s = socket(AF_INET, SOCK_STREAM, 0);
if (s <0){
fprintf(stderr, "Server: Unable to create socket: %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int))<0)
fprintf(stderr, "Server: setsockopt(SO_REUSEADDR) failed: %s", strerror(errno));
if (bind(s,(struct sockaddr*)&addr, sizeof(addr))<0){
fprintf(stderr, "Server: Unable to bind to socket: %s", strerror(errno));
exit(EXIT_FAILURE);
}
if (listen(s,1)<0){
fprintf(stderr, "Server: Unable to listen: %s", strerror(errno));
exit(EXIT_FAILURE);
}
printf("Server: Listening on TCP port %u
", port);
return s;
}
int main(int argc, char *argv[]){
struct sockaddr_in client_addr;
int listensockfd, clientfd;
unsigned int port;
unsigned int len = sizeof(client_addr);
char client_addr_str[INET_ADDRSTRLEN];
char buffer[BUFFER_SIZE];
int nbytes_read;
int nbytes_written;
switch(argc){
case 1:
port = DEFAULT_PORT;
break;
case 2:
port = atoi(argv[1]);
break;
default:
fprintf(stderr, "Usage: server (optional)
");
exit(EXIT_FAILURE);
}
listensockfd = create_socket(port);
while(true){
clientfd = accept(listensockfd,(struct sockaddr *) &client_addr, &len);
if (clientfd <0){
fprintf(stderr, "Server: Error accepting TCP connection: %s
",
strerror(errno));
continue;
} else {
inet_ntop(AF_INET, (struct in_addr*) &client_addr.sin_addr,
client_addr_str, INET_ADDRSTRLEN);
printf("Server: Established TCP connection with client (%s) on port %u
",
client_addr_str, port);
}
bzero(buffer, BUFFER_SIZE);
nbytes_read = read(clientfd, buffer, BUFFER_SIZE);
if (nbytes_read <0)
fprintf(stderr, "Server: Error reading from socket: %s
", strerror(errno));
else
printf("Server: Message received from client: \"%s\"
", buffer);
//*************************************************************
// ADD YOUR CODE HERE
//*************************************************************
printf("Server: Terminating TCP connection with client (%s)
", client_addr_str);
close(clientfd);
}
return EXIT_SUCCESS;
}
------------client.c----------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEFAULT_PORT 4433
#define MAX_HOSTNAME_LENGTH 256
#define BUFFER_SIZE 256
int create_socket(char* hostname, unsigned int port){
int sockfd;
struct hostent* host;
struct sockaddr_in dest_addr;
host = gethostbyname(hostname);
if (host == NULL){
fprintf(stderr, "Client: Cannot resolve hostname %s
", hostname);
exit(EXIT_FAILURE);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd <0){
fprintf(stderr, "Client: Unable to create socket: %s", strerror(errno));
exit(EXIT_FAILURE);
}
dest_addr.sin_family=AF_INET;
dest_addr.sin_port=htons(port);
dest_addr.sin_addr.s_addr =*(long*)(host->h_addr);
if (connect(sockfd,(struct sockaddr *) &dest_addr,
sizeof(struct sockaddr))<0){
fprintf(stderr, "Client: Cannot connect to host %s [%s] on port %d: %s
",
hostname, inet_

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 Programming Questions!