Java Reference
In-Depth Information
Example 5•6: SimpleProxyServer.java (continued)
while((bytes_read = from_server.read(reply)) != -1) {
to_client.write(reply, 0, bytes_read);
to_client.flush();
}
}
catch(IOException e) {}
// The server closed its connection to us, so we close our
// connection to our client.
// This will make the other thread exit.
to_client.close();
}
catch (IOException e) { System.err.println(e); }
finally { // Close the sockets no matter what happens.
try {
if (server != null) server.close();
if (client != null) client.close();
}
catch(IOException e) {}
}
}
}
}
Networking with Applets
As I just described, untrusted applets have strict restrictions placed on the kind of
networking they can do. They are allowed to connect to and accept connections
only from the one host from which they were loaded. No other networking is
allowed. This still leaves open some interesting applet networking possibilities,
though, and Example 5-7 illustrates one of them. We'll discuss applets and the
graphical user interfaces they display in Chapter 15, Applets , and Chapter 10,
Graphical User Interfaces . You shouldn't expect to understand all of this example
until you've read those chapters. Nevertheless, this example does contain interest-
ing networking code, primarily in its run() method.
Þnger is a program and network service that used to be run on almost all Unix
machines. It allows a client to connect over the network and obtain a list of the
users logged on to another system. It may also allow a client to obtain detailed
information (such as telephone numbers) for individual users. Nowadays, with
heightened security concerns, many Unix machines don't provide this service.
The applet shown in Example 5-7 is a client for the Þnger service. Suppose that
I'm an old Unix hacker, and I think that the Þnger service is a great thing. I run the
Þnger server on my workstation and encourage my friends to use it to find out
when I'm logged on. Unfortunately, my friends work on PCs, so they don't have
access to the old-fashioned, text-based Þnger client that all Unix machines have.
The Who applet in Example 5-7 solves this problem. It connects to the appropriate
port on my host and tells them whether I'm logged on or not. Of course, for this
to work, the applet has to be served from my host as well. I might use the Sim-
pleProxyServer program from Example 5-6 to make my computer into a proxy
Search WWH ::




Custom Search