A Network-Based Printer A Socket-Based Printer Server

In the previous two chapters, we covered the basics of using streams and sockets. In this chapter, well use what we have learned to build a simple server application. Along the way, well confront many of the problems that distributed applications face. And our solutions will help to introduce and explain most of the basic RMI infrastructure.

3.1 A Network-Based Printer

The application were going to build is a very simple one; it takes a local printer and makes it available over the network via a socket-based API. Our intended architecture looks like the diagram shown in Figur e 3- 1 . Figure 3-1. A network printer using a socket-based API This figure illustrates three main components: The client application This is a program running on a separate machine somewhere on the network. There is nothing special about the machine this program runs on in fact, many different machines can run this program. It is responsible for presenting a user interface to the user, getting print requests, and sending those requests to the server application. The client application is written entirely in Java and is therefore easy to install on any machine with a JVM. The server application This is a program that resides on a single, designated machine on the network. The machine it runs on is connected locally to a printer. The server applications roles are to receive print requests over the network from the client program, perform whatever intermediate tasks are necessary, and then forward the request to the printer. The printer In this example, were assuming that the printer exists and is activated, and that the code for interfacing a Java program to a local printer has been written. Printer manufacturers are fairly good at providing printer drivers. However, if we implement this part of the application, it could require the use of the Java Native Interface to communicate advanced commands to a printer driver written in C or C++. One consequence of this is that the server application may not entirely be a Java program and, therefore, installing the server might involve significant modifications to the underlying operating system. [ 1] [ 1] For example, installing a printer driver on Windows NT might involve upgrading a system DLL. Figur e 3- 1 is a very vague diagram. Its more of a requirements diagram than an architectural diagram for a networked application. In particular, it doesnt actually say anything about the class structure of our program. Well redraw it later, filling in many more details. Drawing diagrams like this one can be very useful, however, as a means of understanding just what it is that we need to build.

3.2 The Basic Objects