This is in C. For this assignment we will write a simple database server. We wil
ID: 3822355 • Letter: T
Question
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 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
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
Client Program:
#include<winsock2.h>
#include<ws2tcpip.h>
#include<stdio.h>
#include<string.h>
#include<windows.h>
void rec_data(SOCKET hServer);
int nData=0;
typedef struct student {
char lname[ 10 ], initial, fname[ 10 ];
unsigned long SID;
float GPA;
} SREC;
struct SREC database;
int main()
{
float idno=0;
int read=0,count=0;
char idno_s[5]={0};
char pass_con[30]={0};
char password[15]={0};
char data_check[15]={0};
char ipadd[15];
printf("please enter Ip address e.g: 172.16.64.58: ");
scanf("%s",ipadd);
// Initialize WinSock2.2 DLL
// low word = major, highword = minor
WSADATA wsaData = {0};
WORD wVer = MAKEWORD(2,2);
int nRet = WSAStartup( wVer, &wsaData );
if( nRet == SOCKET_ERROR )
{
printf( " Failed to init Winsock library" );
return -1;
}
printf( " Opening connection to server" );
WORD WSAEvent = 0;
WORD WSAErr = 0;
SOCKET hServer = {0};
// open a socket
//
// for the server we do not want to specify a network address
// we should always use INADDR_ANY to allow the protocal stack
// to assign a local IP address
hServer = socket( AF_INET, SOCK_STREAM, IPPROTO_IP );
if( hServer == INVALID_SOCKET )
{
printf( " Invalid socket, failed to create socket" );
return -1;
}
// name a socket
hostent* localHost;
char* localIP;
localHost = gethostbyname("");
localIP = inet_ntoa (*(struct in_addr *)*localHost->h_addr_list);
printf(" %s ",localHost->h_name);
sockaddr_in saServer = {0};
saServer.sin_family = PF_INET;
saServer.sin_port = htons( 10000 );
saServer.sin_addr.s_addr =inet_addr(ipadd);
// Resolve the server address and port
// connect
nRet = connect( hServer, (sockaddr*)&saServer, sizeof( sockaddr ) );
if( nRet == SOCKET_ERROR )
{
printf( " Connection to server failed " );
closesocket( hServer );
return -1;
}
printf( " ================================================" );
printf( " Welcome to student Database" );
printf( " ================================================" );
passagain: printf(" Please enter the Password to login :>> ");
scanf("%s",password);
nData=send(hServer,password,sizeof(password),0);
nData=recv(hServer,pass_con,sizeof(pass_con),0);
if((strcmp(pass_con,"logged on"))==0)
{
printf(" Login Sucessfuly ");
goto down;
}
elseif (count<5)
{
goto passagain;
}
else
goto END;
down: printf(" Please enter Id No of student to view data: ");
scanf("%d",&idno);
sprintf(idno_s,"%f",idno);
nData=send(hServer,idno_s,5,0);
nData=recv(hServer,data_check,sizeof(data_check),0);
if((strcmp(data_check,"Data Found"))==0)
{
printf(" %s ",data_check);
rec_data(hServer);
}
else
printf(" Data Not Found ");
END:
printf( " Closing connection " );
// shutdown socket
nRet = shutdown( hServer, SD_BOTH );
if( nRet == SOCKET_ERROR ) {
// WSAGetLastError()
printf( " Error trying to perform shutdown on socket" );
return -1;
}
// close server socket
nRet = closesocket( hServer );
hServer = 0;
if( nRet == SOCKET_ERROR ) {
printf( " Error failed to close socket" );
}
// Release WinSock DLL
nRet = WSACleanup();
if( nRet == SOCKET_ERROR ) {
printf( " Error cleaning up Winsock Library" );
return -1;
}
return 0;
}
void rec_data(SOCKET hServer)
{
nData=recv(hServer,database.lname,sizeof(database.lname),0);
nData=recv(hServer,database.fname,sizeof(database.fname),0);
nData=recv(hServer,database.SID,sizeof(database.SID),0);
nData=recv(hServer,database.GPA,sizeof(database.GPA),0);
printf(" ID Number :>> %s",database.fname);
printf(" Name :>> %s",database.lname);
printf(" Semester :>> %s",database.SID);
printf(" Discipline :>> %s",database.GPA);
}
Server Program:
#include<winsock2.h>
#include<stdio.h>
#include<windows.h>
#include<string.h>
#include<dos.h>
void send_data(SOCKET hClient, char id_no[6]);
void error(SOCKET hClient);
void wait();
typedef struct student {
char lname[ 10 ], initial, fname[ 10 ];
unsigned long SID;
float GPA;
} SREC;
struct SREC data;
int nData=0;
int main()
{
int count=0 ;
FILE *ptr;
int file_size=0;
float id=0;
int flag=0;
float size=0;
int end=0;
char buf[800] = {0};
char pass[15]={0};
char pass_not[20]="password invalid";
char pass_valid[20]="logged on";
char id_no[6]={0};
printf( " ================================================" );
printf( " Welcome to student Database Server" );
printf( " ================================================" );
WSADATA wsaData = {0}; // Initialize WinSock2.2 DLL
WORD wVer = MAKEWORD(2,2); // low word = major, highword = minor
int nRet = WSAStartup( wVer, &wsaData );
if( nRet == SOCKET_ERROR )
{
// WSAGetLastError()
printf( " Failed to init Winsock library" ); return -1;
}
printf( " Starting server " );
// name a socket
WORD WSAEvent = 0;
WORD WSAErr = 0;
// open a socket
// for the server we do not want to specify a network address
// we should always use INADDR_ANY to allow the protocal stack
// to assign a local IP address
SOCKET hSock = {0};
hSock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( hSock == INVALID_SOCKET )
{
printf( " Invalid socket, failed to create socket" );
return -1;
}
// name socket
sockaddr_in saListen = {0};
saListen.sin_family = PF_INET;
saListen.sin_port = htons( 10000 );
saListen.sin_addr.s_addr = htonl( INADDR_ANY );
// bind socket's name
nRet = bind( hSock, (sockaddr*)&saListen, sizeof(sockaddr) );
if( nRet == SOCKET_ERROR )
{
printf( " Failed to bind socket" );
//shutdown( hSock );
closesocket( hSock );
return -1;
}
while( true )
{
printf( " Listening for connections " ); // listen
nRet = listen( hSock, 5 );// connection backlog queue set to 10
if( nRet == SOCKET_ERROR )
{
int nErr = WSAGetLastError();
if( nErr == WSAECONNREFUSED )
{
printf( " Failed to listen, connection refused" );
}
else
{
rintf( " Call to listen failed" );
}
closesocket( hSock );
return -1;
}
// connect
sockaddr_in saClient = {0};
int nSALen = sizeof( sockaddr );
SOCKET hClient={0};
hClient = accept( hSock, (sockaddr*)&saClient, &nSALen );
if( hClient == INVALID_SOCKET )
{
printf( " Invalid client socket, connection failed " );
closesocket( hSock );
return -1;
}
printf( " Connection estabilished" );
again:nData=recv(hClient,pass,sizeof(pass),0); // sending download msg
if ((strcmp(pass,"NUST"))!=0&& count<5)
{
count++;
Data=send(hClient,pass_not,sizeof(pass_not),0);
goto again;
}
elseif ((strcmp(pass,"NUST"))==0)
{
nData=send(hClient,pass_valid,sizeof(pass_valid),0);
goto down;
}
else
goto END;
down: printf(" :>> User logged in ");
if((ptr=fopen("database.txt","rb+"))==NULL)
{
printf(" File error ");
}
nData=recv(hClient,id_no,sizeof(id_no),0);
id=atof(id_no);
fseek(ptr,0L,SEEK_SET);
do
{
fread(&data,sizeof(data),1,ptr);
if(data.SID==id)
{
flag=1;
break;
}
}while(feof(ptr)==0);
if(flag==1)
{
send_data(hClient,id_no);
flag=0;
nData=recv(hClient,cont,sizeof(cont),0);
if((strcmp(cont,"y"))==0||(strcmp(cont,"Y"))==0)
{
goto again;
}
}
else
{
error(hClient);
}
END:
closesocket( hClient ); // close client connection
end++;
if(end==10)
break;
}
printf( " Shutting down the server" );
nRet = closesocket( hSock ); // close server socket
hSock = 0;
if( nRet == SOCKET_ERROR )
{
printf( " Error failed to close socket" );
}
nRet = WSACleanup(); // Release WinSock DLL
if( nRet == SOCKET_ERROR )
{
printf( " Error cleaning up Winsock Library" );
return -1;
}
printf( " Server is offline" );
return 0; // shut down
}
void send_data(SOCKET hClient,char id_no[6])
{
char d_found[15]="Data Found";
printf(" Sending The Record:>>");
printf(" %d",data.fname);
printf(" %s",data.lname);
printf(" %s",data.SID);
printf(" %s",data.GPA);
nData=send(hClient,d_found,sizeof(d_found),0);
wait();
nData=send(hClient,data.fname,sizeof(data.fname),0);
wait();
nData=send(hClient,data.lname,sizeof(data.lname),0);
wait();
nData=send(hClient,data.SID,sizeof(data.SID),0);
wait();
nData=send(hClient,data.GPA,sizeof(data.GPA),0);
wait();
}
void error(SOCKET hClient)
{
char d_error[20]="Data Not Found";
wait();
nData=send(hClient,d_error,sizeof(d_error),0);
}
void wait()
{
unsignedint delay;
for(delay=0;delay<=100000000;delay++)
{
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.