Computer science Use C language to solve the question. The initial server and client .c code is given write code using these .c client and server files. Q1: Design a client-server model for two-way...








Computer science



Use C language to solve the question. The initial server and client .c code is given write code using these .c client and server files.


Q1:



Design a client-server model for two-way communication. Both the client and server should be
able to send and receive messages.
Upon the establishment of successful connection, the server should send a message, “Hi, you
have connected to the server!”.
[The client should then send a string to the server. The server should reverse the string and
send it back to the client. The client should then display the received string.] This job will be
done in a loop of 5 times.


--------------------------------------------



server.c


#include #include #include #include #include int main() { char server_message[256] = "Hi, Yes you have reached the server!"; char buf[200]; // create the server socket int server_socket; server_socket = socket(AF_INET, SOCK_STREAM, 0); // define the server address struct sockaddr_in server_address; server_address.sin_family = AF_INET; server_address.sin_port = htons(3001); server_address.sin_addr.s_addr = INADDR_ANY; // bind the socket to our specified IP and port bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address)); listen(server_socket, 5); int client_socket; client_socket = accept(server_socket, NULL, NULL); // send the message recv(client_socket, &buf, sizeof(buf), 0); printf("\n %s \n", buf); send(client_socket, server_message, sizeof(server_message), 0); // close the socket close(server_socket); return 0; }



client.c


#include #include #include #include #include int main() { char request[256] = "Hello I am Client are you there"; char buf[200]; // create the socket int sock; sock = socket(AF_INET, SOCK_STREAM, 0); //setup an address struct sockaddr_in server_address; server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = INADDR_ANY; server_address.sin_port = htons(3001); connect(sock, (struct sockaddr *) &server_address, sizeof(server_address)); send(sock, request, sizeof(request), 0); recv(sock, &buf, sizeof(buf), 0); printf("\n %s \n", buf); close(sock); return 0; }





Jun 03, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here