For this assignment, you will write a client and a server. The server must be able to handle multiple simultaneous clients. The server should spawn a separate thread for each client (forking is not permitted). The clients will be able to query a database on the server (the actual database queries will be simulated.) The database will have information about the states. Queries sent by the client will be very simple. The client will send the name of a state, and the database will return the capital of that state. If the state is not in the database, the server will return the string "Not found". Unfortunately, these queries will take a long time, and the server has to make sure that only one client at a time is using the database.
The client
The client will take two arguments, the name of the computer on which the server is running, and the port number. Once a connection is established, it will display a prompt to the user, and the user will enter state names. These will be sent to the server and the server will reply with the capital, which will be displayed on the terminal of the client. This continues until the user enters ``quit'', which will break the connection to the server.
You must submit two versions of your client, one for WIN32 and one for Unix.
The server
The server will take one argument, the port number. It will listen for connections. Whenever a connection is established, it will display a message showing the ip address of the client. It should spawn off a separate thread for each connection. This thread should listen for database queries, and whenever it receives one, it should call the function GetCapital (this function is provided for you). This function returns the capital of the state of its argument, or a null pointer if the state is not found in the database. This function displays the start and the end of the query, and each query takes ten seconds. You need to make sure that only one query at a time is being handled by the database, in other words, the message Searching for statename must be followed by Searching for statename complete.
You should write two versions of your server, one for win32 and one for Unix.
Note that you will need to submit four separate programs, a client and a server for Unix and a client and server for WIN32.
Here is the code the GetCapitals function. Note that I was lazy so it only has 12 states.
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h> /* for sleep */
#endif
#define NUMSTATES 12
#define searchtime 10
char *thestates[]={"Maine","New Hampshire","Vermont",
"Massachusetts", "Rhode Island", "Connecticut",
"New York", "New Jersey", "Pennsylvania", "Ohio",
"Maryland", "Indiana"};
char *thecapitals[]={"Augusta","Concord","Montpelier",
"Boston", "Providence", "Hartford", "Albany",
"Trenton", "Harrisburg", "Columbus",
"Annapolis", "Indianapolis"};
char *GetCapital(char *state)
{
int i;
char *result = NULL;
printf("Searching for %s\n",state);
#ifdef WIN32
Sleep(10000);
#else
sleep(searchtime);
#endif
for (i=0;i<NUMSTATES;i++) {
if (strcmp(state,thestates[i])==0) {
result = thecapitals[i];
break;
}
}
printf("Searching for %s complete\n",state);
return result;
}