Java Reference
In-Depth Information
Example 5•5: HttpMirror.java (continued)
System.err.println("Usage: java HttpMirror <port>");
}
}
}
A Proxy Server
Example 5-6 shows another network server: a simple, single-threaded proxy
server. A proxy server is one that acts as a proxy for some other real server. When
a client connects to a proxy server, the proxy forwards the client's requests to the
real server, and then forwards the server's responses to the client. To the client,
the proxy looks like the server. To the real server, the proxy looks like a client.
Why bother with a proxy server at all? Why can't a client just connect directly to
the real server? First and foremost, there are firewall-related reasons to use proxy
servers. There are also interesting filtering applications of such servers; a sophisti-
cated proxy web server might strip advertising out of the web pages it downloads,
for example. There is yet another reason to use proxy servers that arises when
using Java networking capabilities with applets, as the following scenario makes
clear.
Suppose that I have developed a nifty new server that runs on my computer
oxymor on.oreilly.com
. Now I write an applet client for my service and make the
applet available on the Web by publishing it on the
www.or eilly.com
web server.
Right away there is a problem. Applet security restrictions allow an applet to
establish network connections to only one host: the host from which it was down-
loaded. So my applet client downloaded from
www.or eilly.com
can't communicate
with my incredibly useful server on
oxymor on.oreilly.com
. What can I do? The sys-
tem administrator won't let me install my new server on
www.or eilly.com
, and I
don't want the overhead of running Apache or some other web server on my
desktop workstation,
oxymor on
. So instead, I run a simple proxy server, like the
one in Example 5-6, on
oxymor on
.Iset it up so that it listens for connections on
port 4444 and acts as a proxy for the web server running on port 80 of
www.or eilly.com
. I publish my applet at
http://www.or eilly.com/staff/david/
nifty.html
, but tell people to load it from
http://oxymor on.oreilly.
com:4444/staff/david/nifty.html
.
*
This solves the problem. As far as the applet is
concerned, it has been loaded from
oxymor on.oreilly.com
, so it can connect to my
nifty service running on that host.
There are not really any new features in Example 5-6. It is an interesting example
because it combines the features of both client and server into one program.
When studying this code, remember that the proxy server mediates the connection
between a client and a server. It acts like a server to the client and like a client to
the server.
SimpleProxyServer
is a single-threaded server; it can only handle one
client connection at a time. Nevertheless, you'll notice that it does use a thread
(implemented in an anonymous inner class) so that the proxy can transfer data
from client to server and from server to client at the same time. In this example,
*
This is a fictitious example; these URLs don't really do anything!