Finally, down to the program itself. You are to build a simple database
lookup program. On the server side, you will have a file of the following
form:
| Columns
| Content
| Example
|
| 1-20
| Name
| Donald M. Duck
|
| 22-28
| Hourly wage
| 11.35
|
| 30-31
| Hours worked
| 40
|
The client will query the user for a name and send a request to the server.
The server will search this file for a given name and return the hourly wage
and hours worked to the client. The client will output the name and the
total pay (hourly wage times the hours worked). This is an incredibly simple
DB problem, but the networking problems are not so simple.
First, the server and client may be running on machines with different
storage formats (Big Endian vs. Little Endian), so you have to insure that
all numeric values are transmitted in a common format. This is typically
network byte order, which on the Internet means Big Endian. The routines
to handle this conversion are htons, htonl, ntohs and ntohl (and there are
others).
Unfortunately, because floating point values vary in storage scheme, there is
no network byte order for them, so you have to invent your own. In this
case, what you should use is the following:
bytes item format
------------------------------------
0-3 mantissa long int
4-7 characteristic long int
The handling of floating point numbers is often done this way. If you
have the value 16.45, the frexpf function will convert it into a
floating point mantissa and characteristic (which would be 0.1645 and 2
in base 10, although frexp provides the characteristic in base 2). Then, you
can right shift the decimal point for the mantissa 31 places to get an integer.
On the receiving end, scalbf can be used to reverse the frexp process.
Since the server might detect an error in the search process, it also needs
to be able to return an error. This can be accomplished by adding
a state value to the return message, so that the format looks like this.
bytes item format
------------------------------------
0 state "0" = OK, "1" = Error
1-4 mantissa long int (4 bytes)
5-8 characteristic long int (4 bytes)
9-10 hours worked short int (2 bytes)
The data for the database is here .
Test your program for the following cases and
turn in the source and the test runs. Don't worry about error checking on
the input. Assume it either matches the name exactly, or the search fails.
Howdy Dootie
Rocky Squirrel
Pillsbury T Doughboy
Dilbert