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 server/client 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 "tcpSC.h"
#if defined(_WIN32)
#include
#endif
int main(int argc, char *argv[]){
#if defined(_WIN32)
WSADATA d;
if (WSAStartup(MAKEWORD(2,2), &d)){
fprintf(stderr, "Failed to initialize.
");
return 1;
}
#endif
if (argc <3){
fprintf(stderr, "usage: tcp_client hostname port
");
return 1;
}
printf("Configuring remote address...
");
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
struct addrinfo *peer_address;
if (getaddrinfo(argv[1], argv[2], &hints, &peer_address)){
fprintf(stderr, "getaddrinfo() failed. (%d)
", GETSOCKETERRNO());
return 1;
}
printf("Remote address is: ");
char address_buffer[100];
char service_buffer[100];
getnameinfo(peer_address->ai_addr, peer_address->ai_addrlen,
address_buffer, sizeof(address_buffer),
service_buffer, sizeof(service_buffer),
NI_NUMERICHOST);
printf("%s %s
", address_buffer, service_buffer);
printf("Creating socket...
");
SOCKET socket_peer;
socket_peer = socket(peer_address->ai_family,
peer_address->ai_socktype, peer_address->ai_protocol);
if (!ISVALIDSOCKET(socket_peer)){
fprintf(stderr, "socket() failed. (%d)
", GETSOCKETERRNO());
return 1;
}
printf("Connecting...
");
if (connect(socket_peer,
peer_address->ai_addr, peer_address->ai_addrlen)){
fprintf(stderr, "connect() failed. (%d)
", GETSOCKETERRNO());
return 1;
}
freeaddrinfo(peer_address);
printf("Connected.
");
printf("To send data, enter text followed by enter.
");
while(1){
fd_set reads;
FD_ZERO(&reads);
FD_SET(socket_peer, &reads);
#if !defined(_WIN32)
FD_SET(0, &reads);
#endif
struct timeval timeout;
timeout.tv_sec =0;
timeout.tv_usec =100000;
if (select(socket_peer+1, &reads, 0,0, &timeout)<0){
fprintf(stderr, "select() failed. (%d)
", GETSOCKETERRNO());
return 1;
}
if (FD_ISSET(socket_peer, &reads)){
char read[4096];
int bytes_received = recv(socket_peer, read, 4096,0);
if (bytes_received <1){
printf("Connection closed by peer.
");
break;
}
printf("Received (%d bytes): %.*s",
bytes_received, bytes_received, read);
}
#if defined(_WIN32)
if(_kbhit()){
#else
if(FD_ISSET(0, &reads)){
#endif
char read[4096];
if (!fgets(read,4096, stdin)) break;
printf("Sending: %s", read);
int bytes_sent = send(socket_peer, read, strlen(read),0);
printf("Sent %d bytes.
", bytes_sent);
}
}//end while(1)
printf("Closing socket...
");
CLOSESOCKET(socket_peer);
#if defined(_WIN32)
WSACleanup();
#endif
printf("Finished.
");
return 0;
}
Here is the server code:
#include "tcpSC.h"
#include
int main(){
#if defined(_WIN32)
WSADATA d;
if (WSAStartup(MAKEWORD(2,2), &d)){
fprintf(stderr, "Failed to initialize.
");
return 1;
}
#endif
printf("Configuring local address...
");
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
struct addrinfo *bind_address;
getaddrinfo(0,"8080", &hints, &bind_address);
printf("Creating socket...
");
SOCKET socket_listen;
socket_listen = socket(bind_address->ai_family,
bind_address->ai_socktype, bind_address->ai_protocol);
if (!ISVALIDSOCKET(socket_listen)){
fprintf(stderr, "socket() failed. (%d)
", GETSOCKETERRNO());
return 1;
}
printf("Binding socket to local address...
");
if (bind(socket_listen,
bind_address->ai_addr, bind_address->ai_addrlen)){
fprintf(stderr, "bind() failed. (%d)
", GETSOCKETERRNO());
return 1;
}
freeaddrinfo(bind_address);
printf("Listening...
");
if (listen(socket_listen, 10)<0){
fprintf(stderr, "listen() failed. (%d)
", GETSOCKETERRNO());
return 1;
}
fd_set master;
FD_ZERO(&master);
FD_SET(socket_listen, &master);
SOCKET max_socket = socket_listen;
printf("Waiting for connections...
");
while(1){
fd_set reads;
reads = master;
if (select(max_socket+1, &reads, 0,0,0)<0){
fprintf(stderr, "select() failed. (%d)
", GETSOCKETERRNO());
return 1;
}
SOCKET i;
for(i =1; i <= max_socket; ++i){

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!