Question

Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple sHere are the details of the client-server interaction: server-host> server server-port Client-host> client server-host serve-Notes. Include extra printf statements to print what messages are sent or received at the server and client sites. So we can

Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array of struct student_info ( char abc123171 char name [101 double GPA; To simplify the tasks, the server should create a static array of 10 students with random abc123, name, and GPA in the server program. Then the server waits for clients. When a client connects to the server, the server creates a child process. The parent process waits for other clients while the child process serves the client as follows The child (server) can get two types of messages: "GETGPA abc23" or "STOP". If the child ets "GETGPA abc123" then it sends the name and GPA of the queried student. Or an error msg if abc123 is not in the array/list. The child then waits for the next query until it gets "STOP" msg. In that case, both the client and the child terminate. In addition, you should show how to wait for a signal (SIGINT Ctrl-C) using sigsuspend() in the beginning of the client program.
Here are the details of the client-server interaction: server-host> server server-port Client-host> client server-host serve-port Create a static array of struct student_info with Print a message that client is waiting for SIGINT Ctrl-C 10 random entries wait for Ctrl-C.. Print a message that Ctri-C is pressed.. The server always waits for TCP connection eguests in an infinite loo Upon receiving a TCP connection request, the server (parent process) creates a child process, the server. which serves the client. The parent process will continue to wait for another client. The client will first establish a TCP connection with Child server process The client gets an input from the user: 1. Get GPA for a student 2. Stop Wait for a request from the client If the server gets "GETGPA abc123" If the user enters option 1, Check if the given abc123 is in the array If so get abc123 from the user and send "GETGPA abc123" request to the server. create a reply message containing student wait for a reply and name and gpa, and send it to the client print it. If not, send an error message If the user enters option 2, If the server gets "STOP" sent "STOP" request to the server. close connection and quit. close connection and quit.
Notes. Include extra printf statements to print what messages are sent or received at the server and client sites. So we can easily follow your protocol. Make sure you close the socket descriptors that are not needed in the parent and child. You don't need to handle every possible error, but make sure you check what each system call returns etc. and accordingly take minimum action (e.g., simply quit) · .
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please comment if you find any difficukty

Server.c:

----------------------------------------------------------***********************-----------------------------------------------------------

#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>

void function(int);

struct student_info {
char abc123[7];
char name[10];
double GPA;
}students[10];

void createStaticEntries(){
strcpy(students[0].abc123, "ab");
strcpy(students[0].name, "Abhijeet");
students[0].GPA = 3.2;

strcpy(students[1].abc123, "cd");
strcpy(students[1].name, "Sonal");
students[1].GPA = 3.7;

strcpy(students[2].abc123, "ef");
strcpy(students[2].name, "Seth");
students[2].GPA = 4.0;
}
void main()
{
int sock_ser_fd, sock_cli_fd;
int len, pid;
struct sockaddr_in server, client;

if((sock_ser_fd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
printf("\nServer: Socket Error: %d",errno);
fflush(stdout);
exit(0);
}

createStaticEntries();

bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(5000);

if(bind(sock_ser_fd, (struct sockaddr *)&server,sizeof(server)) == -1)
{
printf("\nServer: Binding Error: %d",errno);
fflush(stdout);
exit(0);
}

printf("\nServer Ready\n");
fflush(stdout);
if(listen(sock_ser_fd,5) == -1)
{
printf("\nServer: listen Error: %d",errno);
fflush(stdout);
exit(0);
}

len = sizeof(client);
while(1)
{
printf("\nServer: Starting accept");
if((sock_cli_fd = accept(sock_ser_fd,(struct sockaddr *)&client, &len)) == -1)
{
printf("\nServer: accept Error: %d",errno);
fflush(stdout);
exit(0);
}
pid = fork();
if(pid < 0)
{
printf("\nError in fork()");
fflush(stdout);
exit(0);
}
if(pid == 0)
{
close(sock_ser_fd);
function(sock_cli_fd);
printf("\nServer: Closing connection");
fflush(stdout);
exit(0);
}
else
{
close(sock_cli_fd);
}
}
}

void function(int sockfd){
char buffer[20];
int i, flag = 0;
char *p;

bzero(buffer,100);
recv(sockfd, buffer, 20, 0);

p = strtok (buffer," ");

if(strcmp("STOP", p) == 0){

close(sockfd);
return;
}

p = strtok (NULL, " ");

//search for abc123 of the student in the array
for(i = 0; i<10; i++){
if(strcmp(students[i].abc123, p) == 0){
printf("\nStudent found");
fflush(stdout);
bzero(buffer, 100);
sprintf(buffer, "%s %f", students[i].name, students[i].GPA);
send(sockfd, buffer, strlen(buffer), 0);
flag = 1;
}
}
if(flag != 1){
printf("\nStudent not found!");
fflush(stdout);
bzero(buffer, 100);
strcpy(buffer, "Student information not found!");
send(sockfd, buffer, strlen(buffer), 0);
}
close(sockfd);
}

------------------------------------------------------------*********************-----------------------------------------------------------------------------------

Client.c:

----------------------------------------------------*************************-----------------------------------------------------------------------------

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<strings.h>
#include<string.h>


void getGPA(int sockfd, char abc123[])
{
char buffer[20], reply[100];
sprintf(buffer, "GETGPA %s", abc123);
p
send(sockfd, buffer, 20, 0);
recv(sockfd, reply, 100, 0);
printf("\nResponse from the server: %s", reply);
fflush(stdout);
}

void main()
{
int sockfd;
struct sockaddr_in server;
char abc123[7];
int ch;

printf("\nClient: Creating socket");
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("\nClient: Socket Error: %d",errno);
fflush(stdout);
exit(0);
}
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("0.0.0.0");
server.sin_port = htons(5000);

printf("\nClient: Connecting to Server...");
if(connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1)
{
printf("\nClient: Connect Error: %d",errno);
fflush(stdout);
exit(0);
}
printf("\nClient: CONNECTED TO SERVER :-)");
fflush(stdout);

//Initial Interface
LABEL:
printf("\n1.Get GPA for a Student.\n2.Stop.");
fflush(stdout);
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the abc123 of the Student: ");
fflush(stdout);
scanf("%s", abc123);
getGPA(sockfd, abc123);
goto LABEL;
break;
case 2:
send(sockfd, "STOP", 20, 0);
break;
default:
printf("\nOops!! Wrong choice!!");
fflush(stdout);
goto LABEL;
}
close(sockfd);
}
-----------------------------------------------------------****************************************------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is in C. For this assignment we will write a simple database server. We will...

    This is in C. For this assignment we will write a simple database server. We will be creating a simple database of student records, so let’s describe these first. The format of a student record is as follows: typedef struct student {     char lname[ 10 ], initial, fname[ 10 ];     unsigned long SID;     float GPA; } SREC; Part One – the Server We will create a database server. The job of the server is to accept a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT