Question: This assignment is due on Saturday and I need help right away. Instructions: Develop a C program that works with sockets by creating a simple
This assignment is due on Saturday and I need help right away.
Instructions:
Develop a C program that works with sockets by creating a simple serverclient setup. The sever and client code are below. I need the modified code to be as close to the original code as possible! Only change the code enough to implement the modifications listed below please.
Project Implementation:
Modify the client to:
Accept input from a user on STDIN
Put this in a loop and send until the user quits the client by sending a value of
quit
Send the string to the server, which will send it back
Print the value received back from the server.
Modify the server to:
Print the value received from the user before sending it back
When quit is received, stop the server
Here is the client code:
#include tcpSCh
#if definedWIN
#include
#endif
int mainint argc, char argv
#if definedWIN
WSADATA d;
if WSAStartupMAKEWORD &d
fprintfstderr "Failed to initialize.
;
return ;
#endif
if argc
fprintfstderr "usage: tcpclient hostname port
;
return ;
printfConfiguring remote address...
;
struct addrinfo hints;
memset&hints, sizeofhints;
hints.aisocktype SOCKSTREAM;
struct addrinfo peeraddress;
if getaddrinfoargv argv &hints, &peeraddress
fprintfstderr "getaddrinfo failed. d
GETSOCKETERRNO;
return ;
printfRemote address is: ;
char addressbuffer;
char servicebuffer;
getnameinfopeeraddressaiaddr, peeraddressaiaddrlen,
addressbuffer, sizeofaddressbuffer
servicebuffer, sizeofservicebuffer
NINUMERICHOST;
printfs s
addressbuffer, servicebuffer;
printfCreating socket...
;
SOCKET socketpeer;
socketpeer socketpeeraddressaifamily,
peeraddressaisocktype, peeraddressaiprotocol;
if ISVALIDSOCKETsocketpeer
fprintfstderr "socket failed. d
GETSOCKETERRNO;
return ;
printfConnecting
;
if connectsocketpeer,
peeraddressaiaddr, peeraddressaiaddrlen
fprintfstderr "connect failed. d
GETSOCKETERRNO;
return ;
freeaddrinfopeeraddress;
printfConnected
;
printfTo send data, enter text followed by enter.
;
while
fdset reads;
FDZERO&reads;
FDSETsocketpeer, &reads;
#if definedWIN
FDSET &reads;
#endif
struct timeval timeout;
timeout.tvsec ;
timeout.tvusec ;
if selectsocketpeer &reads, &timeout
fprintfstderr "select failed. d
GETSOCKETERRNO;
return ;
if FDISSETsocketpeer, &reads
char read;
int bytesreceived recvsocketpeer, read, ;
if bytesreceived
printfConnection closed by peer.
;
break;
printfReceived d bytes: s
bytesreceived, bytesreceived, read;
#if definedWIN
ifkbhit
#else
ifFDISSET &reads
#endif
char read;
if fgetsread stdin break;
printfSending: s read;
int bytessent sendsocketpeer, read, strlenread;
printfSent d bytes.
bytessent;
end while
printfClosing socket...
;
CLOSESOCKETsocketpeer;
#if definedWIN
WSACleanup;
#endif
printfFinished
;
return ;
Here is the server code:
#include tcpSCh
#include
int main
#if definedWIN
WSADATA d;
if WSAStartupMAKEWORD &d
fprintfstderr "Failed to initialize.
;
return ;
#endif
printfConfiguring local address...
;
struct addrinfo hints;
memset&hints, sizeofhints;
hints.aifamily AFINET;
hints.aisocktype SOCKSTREAM;
hints.aiflags AIPASSIVE;
struct addrinfo bindaddress;
getaddrinfo &hints, &bindaddress;
printfCreating socket...
;
SOCKET socketlisten;
socketlisten socketbindaddressaifamily,
bindaddressaisocktype, bindaddressaiprotocol;
if ISVALIDSOCKETsocketlisten
fprintfstderr "socket failed. d
GETSOCKETERRNO;
return ;
printfBinding socket to local address...
;
if bindsocketlisten,
bindaddressaiaddr, bindaddressaiaddrlen
fprintfstderr "bind failed. d
GETSOCKETERRNO;
return ;
freeaddrinfobindaddress;
printfListening
;
if listensocketlisten,
fprintfstderr "listen failed. d
GETSOCKETERRNO;
return ;
fdset master;
FDZERO&master;
FDSETsocketlisten, &master;
SOCKET maxsocket socketlisten;
printfWaiting for connections...
;
while
fdset reads;
reads master;
if selectmaxsocket &reads,
fprintfstderr "select failed. d
GETSOCKETERRNO;
return ;
SOCKET i;
fori ; i maxsocket; i
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
