A Proxy Server
Example 5-9 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. This program allows you to create fake services on your machine that are just proxies for real services running somewhere else. (Note that this program is not the same as an HTTP proxy server, which parses a client’s HTTP request and redirects to the appropriate server; this program is a proxy for one specific server, and treats client requests and server responses as opaque streams of bytes.)
There are not any major new networking features in Example 5-9. 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 one thread can block while waiting for input from the server, while the other thread blocks while waiting for input from the ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.