eHow launches Android app: Get the best of eHow on the go.

How To

How to Create a Proxy Server in Java

Contributor
By Javanx3d
eHow Contributing Writer
(2 Ratings)
Java Programming Language Logo
Java Programming Language Logo
WIkimedia Commons

Proxy servers provide the function of acting as a forwarding server between a client computer and another server. Whenever a person or other computer makes a request of the remote server, the proxy computer server will forward both the client computer request as well as the remote server response back. Both the computers having the data forwarded to them do not know that the proxy is making the connections for both. As a result, the proxy server has to be able to fill both the client (request and receive) and the server (respond to requests) roles interchangeably.

From Quick Guide: All About Java Programming
Difficulty: Moderate
Instructions

Things You'll Need:

  • Java development environment
  1. Step 1

    Download and install the latest java standard developer's kit from Sun Microsystems, if not already installed on your computer (see Resources below). Open a text editor or your integrated development environment. In order to support the cross-compatibility between the client and server roles that the proxy server will need to fulfill, a common interface is defined to ensure data compatibility. The Java IO and Net libraries are imported and three common methods are defined for the Proxy server to implements:
    import java.io.*;
    import java.net.*;
    interface mySockets
    {
    String readLine();
    void wrtieLine(String myString);
    void dispose();
    }

  2. Step 2

    Define the Prozy class by implementing the SocketInterface. The class constructor takes three arguments: 1 - The Host IP address, port and whether it should wait for a connection or not.
    public class SocketProxy implements mySockets
    {
    private Socket mySocket;
    private BufferedReader myIn;
    private PrintWriter myOut;
    public SocketProxy( String myHost, int myPort, boolean myWait )
    {

  3. Step 3

    Wait for a new connection to be established. Once a valid connection is established, a BufferedReader input stream is opened and passed to a PrintWriter class output stream, which will be used to forward the information.
    try {
    if (myWait) {
    ServerSocket myServer = new ServerSocket( myPort );
    mySocket = myServer.accept();
    }
    else
    mySocket = new Socket( myHost, myPort );
    myIn = new BufferedReader( new InputStreamReader(
    mySocket.getInputStream()));
    myOut = new PrintWriter( mySocket.getOutputStream(), true );
    } catch( IOException e ) { e.printStackTrace(); }
    }

  4. Step 4

    Use the readLine metod is to read the input stream and return to the writeLine method, which is used to pass the information to the output stream to be forwarded on to the client (or receiving) computer.
    public String readLine() {
    String myString = null;
    try { myString = myIn.readLine();
    } catch( IOException e ) { e.printStackTrace(); }
    return myString;
    }
    public void writeLine( String myString ) {
    myOut.println( myString );
    }

  5. Step 5

    Close the network socket when the Proxy server is done with passing information between the client and server connections.
    public void dispose() {
    try {
    mySocket.close();
    } catch( IOException e ) { e.printStackTrace(); }
    } }

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics