$29.99
Program 5
For this assignment we will write a simple database server.
Feel free to use the socket starter code provided with this assignment: “server.c” and “client.c”.
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.
I will put your port range assignment in your A5 grading feedback. Your grade and the feedback will be updated once I finished grading your submission.
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.
Note:
Write your program in two source code files: “server.c” for the server, “client.c” for the client.
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
If the compiler generates any errors or warnings you will be penalized.