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( ); } }
Apart from the user interface, the client application is equally straightforward. Our user interface is shown in Figure 3-2.
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 ...
No credit card required