O'Reilly logo

Java RMI by William Grosso

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Application Itself

Once we’ve written the data objects and the objects that encapsulate the network protocol, writing the rest of the application is easy. The server doesn’t even need a user interface; it consists of the main( ) function, which instantiates a printer, creates an instance of ServerNetworkWrapper, and then calls accept( ) on the instance of ServerNetworkWrapper:

public static void main(String args[]) {
	try {
		File logfile = new File("C:\\temp\\serverLogfile");
		OutputStream outputStream = new FileOutputStream(logfile);
		Printer printer = new NullPrinter(outputStream);
		ServerNetworkWrapper serverNetworkWrapper = new
              ServerNetworkWrapper(printer);
		serverNetworkWrapper.accept(  );
	}
	catch (Exception e) {
		e.printStackTrace(  );
	 }
}

Writing the Client

Apart from the user interface, the client application is equally straightforward. Our user interface is shown in Figure 3-2.

The user interface for the printer client

Figure 3-2. The user interface for the printer client

The Choose File button uses a JFileChooser to let the user select a file (whose name is displayed in the text area). All the network communication is done using ActionListener, which has been added to the Print File button. And all ActionListener does is instantiate ClientNetworkWrapper and call sendDocumenttoPrinter( ):

private class PrintFile implements ActionListener { public void actionPerformed(ActionEvent event) { try { ClientNetworkWrapper ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required