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!

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> 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 <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> 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; }
 
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Risk Management
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education