7.4 A Sample Client
At this point, we have a data logging application capable of capturing data and serving up to any client over an Ethernet network connection. To test the data logger, we'll develop a small client application to connect to the server and download its current log.
The DataLoggerClient class, shown in Listing 7.9, is a simple command line application that can be run on any Java platform. The name of the server, the TINI running the DataLogger application, is extracted from the first argument on the command line. DataLoggerClient then uses the server name to establish a connection to the server. After the connection has been established, the getInputStream method is invoked on the socket instance to get a stream that can be used for uploading the log information from the server. The input stream is buffered, and the result is wrapped in a DataInputStream. Now the client is ready to read the data in the same format in which it is written by the server.
Listing 7.9 DataLoggerClient
import java.io.*; import java.net.*; import java.util.Date; class DataLoggerClient { static final int PORT = 5588; public static void main(String[] args) { if (args.length != 1) { System.out.println("Usage: java DataLoggerClient server"); System.exit(1); } Socket s = null; DataInputStream din = null; try { s = new Socket(InetAddress.getByName(args[0]), PORT); din = new DataInputStream( new BufferedInputStream(s.getInputStream())); // Read number of data entries coming our way int entries = din.readInt(); System.out.println("Total readings="+entries); for (int i = 0; i < entries; i++) { System.out.print("Entry " + i + ":" + new Date(din.readLong())); System.out.print(", RH=" + din.readDouble()); System.out.println(", TEMP=" + din.readDouble()); } } catch (IOException ioe) { System.out.println("Error downloading readings:"+ ioe.getMessage()); ioe.printStackTrace(); } finally { try { s.close(); din.close(); } catch (IOException _) {} } } }
The first thing the server sends to us is an integer value that tells the client the number of log entries to expect. After the client has read this value, it can loop through all entries, reading each individual sample. The client simply displays each entry as it's being read, but a real client application would probably be logging this information to a database. The individual fields of each entrytime-stamp, humidity, and temperaturemust be read by the client in the same order they are written by the server. The time stamp is read in as a long and passed to a constructor of the Date class. The toString method of Date is then used to display a human readable date and time. The humidity and temperature measurements are simply read as doubles and displayed.
Now we have both the client and server programs and are ready to run both applications. The server can be launched on TINI using a command line similar to the following.
TINI /> java DataLogger.tini 60 120 & Starting DataLogger ...
A maximum number of 60 samples was specified along with a 120-second delay between each sample. After running the server for several minutes to allow it to acquire a few samples, we can run the client. Here is the sample output for DataLoggerClient that is run on a Win2K machine.
java DataLoggerClient 192.168.0.15 Total readings=3 Entry 0:Fri Feb 02 14:20:41 CST 2001, RH=27.733103869596295, TEMP=23.53125 Entry 1:Fri Feb 02 14:22:42 CST 2001, RH=28.067076700395877, TEMP=23.4375 Entry 2:Fri Feb 02 14:24:42 CST 2001, RH=27.73123954744912, TEMP=23.28125
By examining the time stamp, we can see that each sample was taken just over two minutes apart. If we let the server run more than two hours, it will fill its sample vector, and running the client would result in 60 data samples. If we let the server continue to run for days, weeks, or even months, we would still get 60 samples, but they would always represent readings taken within the last two hours.
In the next section, support will be provided for managing a PPP interface. We can then use the same client we developed in this section to test DataLogger's ability to accept connections over both Ethernet and PPP network interfaces.