Try to fix all errors below and give me an explanation on whats up with my error
ID: 3838041 • Letter: T
Question
Try to fix all errors below and give me an explanation on whats up with my errors.
CSCI 2240
Program 5
For this assignment we will write a simple database server.
Feel free to use the socket starter code found in my directory.
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 connection on a socket and perform one of the following actions:
A “get” command asking for the data to be returned in order, either alphabetical order by last name, alphabetical order by first name, in order by student ID, or in order by grade point average. In the case of GPA, the highest should come first (4.0 down to 0.0, not 0.0 up to 4.0).
A “put” command that will take one new SREC and add it to the database.
A “delete” command that will look up a certain SREC by SID and delete that record from the database.
A “stop” command. This command will save the entire database into a file (you name the file whatever you like). When the program starts again, if the file exists the contents are used to initialize the lists. In this way we can insert records, stop the server, run it again, and the records from before are available again. The data should be saved in the file in binary, by using “fread” and “fwrite” or “read” and “write”, whichever you prefer. Each “sizeof(SREC)” represents one student in the file.
While in memory the records must be maintained on four data structures, ordered by “lname”, “fname”, “SID”, or “GPA”. In the case of the names, use the order returned by “strcmp” as the ordering criteria. You can use singly-linked lists, doubly linked lists, or binary search trees – your choice. (If you choose to do search trees, when deleting a record you can mark the tree nodes as deleted and avoid having to write the actual tree code for a delete.)
Part Two – the Client
The client program asks for an activity, and acts on the activity. The client reads commands from the user, formats them into requests to the server, sends the request, and then takes back replies from the server and displays the results. Here are examples of the commands which correspond to the above, along with comments:
Command
Note
get lname
The server sends back the records, ordered by last name
get fname
The server sends back the records, ordered by first name
get SID
The server sends the records back ordered by SID
get GPA
The server sends back the records by high-to-low GPA.
put Mahoney,Bill,R,12345,4.0
The record is added to the database. There are no spaces in the “data” part of the record, and the fields are split apart by commas. The GPA and SID are guaranteed to be valid values, however note that the names may be longer than the 10 characters allowed by the structure. It is up to you to chop the names if necessary.
delete 12345
The database record with SID 12345 is removed.
stop
The server saves the file and exits. The client exits as well.
The format of commands is exactly as shown, in lower case for the commands and in mixed case for the data. “get” is used, not “Get”, for instance, and there will be exactly one space after the command and before any arguments.
The program should correctly handle the case where the user enters a command that is not in the list.
It is necessary to “pretty print” the results that come back from the server. For example, the output should look like this:
Enter next command> get SID
| SID | Lname | Fname | M | GPA |
+-------+-----------+------------+---+------+
| 33445 | Mahoney | Bill | R | 4.00 |
| 45678 | Cavanaugh | Patrick | Z | 4.00 |
| 67890 | Dough | John | A | 2.67 |
| 88891 | Smythe | Jane | F | 3.78 |
| 99341 | Namethati | Waytoolon | S | 3.81 |
+-------+-----------+------------+---+------+
Enter next command> put Simpson,Homer,A,4,1.3
Enter next command> get SID
| SID | Lname | Fname | M | GPA |
+-------+-----------+------------+---+------+
| 00004 | Simpson | Homer | A | 1.30 |
| 33445 | Mahoney | Bill | R | 4.00 |
| 45678 | Cavanaugh | Patrick | Z | 4.00 |
| 67890 | Dough | John | A | 2.67 |
| 88891 | Smythe | Jane | F | 3.78 |
| 99341 | Namethati | Waytoolon | S | 3.81 |
+-------+-----------+------------+---+------+
The output should look exactly as shown (with different data obviously) including the ‘|’, ‘+’, etc. The “SID” should occupy exactly 5 columns, zero filled if needed, with a space on either side. The names, both the last and first, should occupy exactly nine columns, with a space on either side. The middle initial has a space on each side. The GPA should occupy exactly four columns.
Part Three – the Port Number and usage
It will be necessary to make sure that each student in the class has a unique port number to work from. Also, what happens on a Linux machine if a process dies unexpectedly is that the port remains “in use” for a period of time. Thus we will assign port numbers in ranges, with 10 per student. Suppose your port numbers are 20000-20009. You could write the program so that it uses port 20000 and if that port is in use after a program crashes, you can switch to 20001. The ports remain in use only for a certain amount of time, so assigning things in groups of ten should be fine. From playing around on Loki it seems to take about a minute or so before the socket is once again available.
There is a class list posted on Blackboard along with this assignment that shows your port range assignment.
You do not retrieve the port number from a command line argument, rather create an easy to access (and find for my sake) constant for your port number, for example:
#define PORT_NUM 20000
Neither the Server nor the Client should accept any command line arguments. The port number is going to be previously defined on both sides as discussed above, and your client can assume the host as “loki” or “localhost”.
Hints
It will be necessary for the server to “tell” the client how many records will be coming back on a “get” request. I suggest that when the server receives the request, it first sends back an integer to the client, and then the client and server loop, reading (or sending) each record individually.
I would suggest that in the case of a “put”, the client take the command line and convert it into an SREC and then send the “put” command followed by the record.
For that matter, you might consider having the client send an integer or character representing what action is to be taken. These could include the four types of “get” requests as well.
Hand-in instructions…
Note: After the programs are collected, any compiled program (executable) in that directory is deleted and the program is recompiled for testing. Specifically, make sure you compile the program with the same options that are used for grading:
gcc -Wall –ansi –pedantic –o server server.c
gcc -Wall –ansi –pedantic –o client client.c
or whatever the correct file name is. If the compiler generates any errors or warnings you will be penalized.
/*Name Ryan Sullivan
class : Online Section of 2240
Program # : 5
Due Date : 4/24/17
Honor Pledge: On my honor as a student of the University
of Nebraska at Omaha, I have neither given nor received
unauthorized help on this homework assignment.
NAME: Ryan Sullivan
NUID: 00194486
EMAIL: rksullivan@unomaha.edu
Partners: "None"
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#define PORT_NUM 20240
int getMethod(char *Parameter, int sockfd);
int putMethod(char *Parameter, int sockfd);
int deleteMethod(char *Parameter, int sockfd);
int stopMethod(int sockfd);
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
char *command, *Parameter;
char buffer[256];
sockfd= socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0) error("Error: Socket Not Opened");
/*gets host*/
server = gethostbyname("loki");
if(server == NULL)
{
fprintf(stderr, "Error: No Such Host ");
exit(0);
}
memset((char *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy((char *) server -> h_addr_list, (char *)&serv_addr.sin_addr.s_addr, server -> h_length);
serv_addr.sin_port = htons(PORT_NUM);
if(connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr))<0)
{
error("Error: Did Not Connect");
}
while(1==1)
{
memset(buffer, 0, 256);
printf("Enter your command");
fgets(buffer, 255, stdin);
command = strtok(buffer, " ");
Parameter = strtok(NULL, " ");
if(strcmp(command, "get") == 0)
{
getMethod(Parameter, sockfd);
}
else if(strcmp(command, "put") ==0)
{
putMethod(Parameter, sockfd);
}
else if(strcmp(command, "delete") == 0)
{
deleteMethod(Parameter, sockfd);
}
else if(strcmp(command, "stop ") == 0)
{
stopMethod(sockfd);
exit(0);
}
}/*end*/
return 0;
}
int getMethod(char *Parameter, int sockfd)
{
int n;
char *get = "get";
n = write(sockfd, get, 255);
if(n < 0)
{
error("Error: failed to write");
}
n = write(sockfd, Parameter, strlen(Parameter));
if(n < 0)
{
error("Error: failed to write ");
}
return 0;
}
int putMethod(char *Parameter, int sockfd)
{
char *put = "put";
int ex;
int n;
n= write(sockfd, put, strlen(put));
if(n<0)
{
error("Error: Failed to Write");
}
n=write(sockfd, Parameter, strlen(Parameter));
if(n<0)
{
error("Error: Failed to Write");
}
n = read(sockfd, &ex, sizeof(int));
if(n < 0)
{
error("Error: Failed to read");
}
if(ex == 1)
{
printf("Success");
}
return 0;
}
int deleteMethod(char *Parameter, int sockfd)
{
int n;
int ex;
char *delete = "delete";
n= write(sockfd, delete, strlen(delete));
if(n<0)
{
error("Error: writing to socket error");
}
n=write(sockfd, Parameter, strlen(Parameter));
if(n<0)
{
error("Error: writing to socket error");
}
n= read(sockfd,&ex, sizeof(int));
if(n<0)
{
error("Error: reading from socket error");
}
if(ex==1)
{
printf("Record Deleted ");
}
return 0;
}
int stopMethod(int sockfd)
{
close(sockfd);
return 1;
}
Command
Note
get lname
The server sends back the records, ordered by last name
get fname
The server sends back the records, ordered by first name
get SID
The server sends the records back ordered by SID
get GPA
The server sends back the records by high-to-low GPA.
put Mahoney,Bill,R,12345,4.0
The record is added to the database. There are no spaces in the “data” part of the record, and the fields are split apart by commas. The GPA and SID are guaranteed to be valid values, however note that the names may be longer than the 10 characters allowed by the structure. It is up to you to chop the names if necessary.
delete 12345
The database record with SID 12345 is removed.
stop
The server saves the file and exits. The client exits as well.
Explanation / Answer
server.c
======================================================================
client.c
=============================================================================
student.h
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.