5.9 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 client. In this example, the main thread reads bytes from the server and sends them to the client. A separate thread reads bytes from the client and sends them to the server.

Example 5-9. SimpleProxyServer.java
package je3.net; import java.io.*; import java.net.*; /**  * This class implements a simple single-threaded proxy server.  **/ public class SimpleProxyServer {     /** The main method parses arguments and passes them to runServer */     public static void main(String[  ] args) throws IOException {         try {             // Check the number of arguments             if (args.length != 3)                  throw new IllegalArgumentException("Wrong number of args.");                          // Get the command-line arguments: the host and port we are proxy             // for and the local port that we listen for connections on.             String host = args[0];             int remoteport = Integer.parseInt(args[1]);             int localport = Integer.parseInt(args[2]);             // Print a start-up message             System.out.println("Starting proxy for " + host + ":" +                                 remoteport + " on port " + localport);             // And start running the server             runServer(host, remoteport, localport);   // never returns         }          catch (Exception e) {             System.err.println(e);             System.err.println("Usage: java SimpleProxyServer " +                                "<host> <remoteport> <localport>");         }     }          /**      * This method runs a single-threaded proxy server for       * host:remoteport on the specified local port.  It never returns.      **/     public static void runServer(String host, int remoteport, int localport)          throws IOException {         // Create a ServerSocket to listen for connections with         ServerSocket ss = new ServerSocket(localport);                  // Create buffers for client-to-server and server-to-client transfer.         // We make one final so it can be used in an anonymous class below.         // Note the assumptions about the volume of traffic in each direction.         final byte[  ] request = new byte[1024];         byte[  ] reply = new byte[4096];                  // This is a server that never returns, so enter an infinite loop.         while(true) {             // Variables to hold the sockets to the client and to the server.             Socket client = null, server = null;             try {                 // Wait for a connection on the local port                 client = ss.accept( );                                  // Get client streams.  Make them final so they can                 // be used in the anonymous thread below.                 final InputStream from_client = client.getInputStream( );                 final OutputStream to_client = client.getOutputStream( );                 // Make a connection to the real server.                 // If we cannot connect to the server, send an error to the                  // client, disconnect, and continue waiting for connections.                 try { server = new Socket(host, remoteport); }                  catch (IOException e) {                     PrintWriter out = new PrintWriter(to_client);                     out.print("Proxy server cannot connect to " + host + ":"+                               remoteport + ":\n" + e + "\n");                     out.flush( );                     client.close( );                     continue;                 }                                  // Get server streams.                 final InputStream from_server = server.getInputStream( );                 final OutputStream to_server = server.getOutputStream( );                 // Make a thread to read the client's requests and pass them                 // to the server.  We have to use a separate thread because                 // requests and responses may be asynchronous.                 Thread t = new Thread( ) {                     public void run( ) {                         int bytes_read;                         try {                             while((bytes_read=from_client.read(request))!=-1) {                                 to_server.write(request, 0, bytes_read);                                 to_server.flush( );                             }                         }                         catch (IOException e) {  }                         // the client closed the connection to us, so close our                         // connection to the server.  This will also cause the                          // server-to-client loop in the main thread exit.                         try {to_server.close( );} catch (IOException e) {  }                     }                 };                 // Start the client-to-server request thread running                 t.start( );                   // Meanwhile, in the main thread, read the server's responses                 // and pass them back to the client.  This will be done in                 // parallel with the client-to-server request thread above.                 int bytes_read;                 try {                     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) {  }             }         }     } }


Java Examples in a Nutshell
Java Examples in a Nutshell, 3rd Edition
ISBN: 0596006209
EAN: 2147483647
Year: 2003
Pages: 285

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net