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 DEFAULTPORT
#define BUFFERSIZE
int createsocketunsigned int port
int s;
struct sockaddrin addr;
addr.sinfamily AFINET;
addr.sinport htonsport;
addr.sinaddr.saddr htonlINADDRANY;
s socketAFINET, SOCKSTREAM, ;
if s
fprintfstderr "Server: Unable to create socket: s strerrorerrno;
exitEXITFAILURE;
if setsockopts SOLSOCKET, SOREUSEADDR, &int sizeofint
fprintfstderr "Server: setsockoptSOREUSEADDR failed: s strerrorerrno;
if bindsstruct sockaddr&addr, sizeofaddr
fprintfstderr "Server: Unable to bind to socket: s strerrorerrno;
exitEXITFAILURE;
if listens
fprintfstderr "Server: Unable to listen: s strerrorerrno;
exitEXITFAILURE;
printfServer: Listening on TCP port u
port;
return s;
int mainint argc, char argv
struct sockaddrin clientaddr;
int listensockfd, clientfd;
unsigned int port;
unsigned int len sizeofclientaddr;
char clientaddrstrINETADDRSTRLEN;
char bufferBUFFERSIZE;
int nbytesread;
int nbyteswritten;
switchargc
case :
port DEFAULTPORT;
break;
case :
port atoiargv;
break;
default:
fprintfstderr "Usage: server optional
;
exitEXITFAILURE;
listensockfd createsocketport;
whiletrue
clientfd acceptlistensockfdstruct sockaddr &clientaddr, &len;
if clientfd
fprintfstderr "Server: Error accepting TCP connection: s
strerrorerrno;
continue;
else
inetntopAFINET, struct inaddr &clientaddr.sinaddr,
clientaddrstr INETADDRSTRLEN;
printfServer: Established TCP connection with client s on port u
clientaddrstr port;
bzerobuffer BUFFERSIZE;
nbytesread readclientfd buffer, BUFFERSIZE;
if nbytesread
fprintfstderr "Server: Error reading from socket: s
strerrorerrno;
else
printfServer: Message received from client: s
buffer;
ADD YOUR CODE HERE
printfServer: Terminating TCP connection with client s
clientaddrstr;
closeclientfd;
return EXITSUCCESS;
client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DEFAULTPORT
#define MAXHOSTNAMELENGTH
#define BUFFERSIZE
int createsocketchar hostname, unsigned int port
int sockfd;
struct hostent host;
struct sockaddrin destaddr;
host gethostbynamehostname;
if host NULL
fprintfstderr "Client: Cannot resolve hostname s
hostname;
exitEXITFAILURE;
sockfd socketAFINET, SOCKSTREAM, ;
if sockfd
fprintfstderr "Client: Unable to create socket: s strerrorerrno;
exitEXITFAILURE;
destaddr.sinfamilyAFINET;
destaddr.sinporthtonsport;
destaddr.sinaddr.saddr longhosthaddr;
if connectsockfdstruct sockaddr &destaddr,
sizeofstruct sockaddr
fprintfstderr "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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
