How To Create a Proxy Server in Java

A Java proxy server is a middleman of a client computer or processor and a server. Though it is invisible, it performs a very important function of sending and responding requests from the computer to the server or vice versa. If you want to make a proxy server using Java, here's how you can do it:

  • Install the application. If your computer does not have the latest Sun Microsystems' Java standard kit, download and install it into the computer system.
  • Open the text editor of your choice. This can also be your preferred integrated development environment. Keep in mind that in order for the Java proxy server to perform its duties of cross-compatibility, it must fulfill an interface. In accordance with this, import the Net libraries and Java IO and define the proxy server. The implementation must appear as follows:

                interface mySockets
                String readLine();
                void wrtieLine(String myString);
                void dispose();

  • Check for the validity of different parameters. When the server is initiated, the legality or validity of different parameters will be displayed. The parameters that are included are the local port, name of the remote host, remote port address, and the like. If there are invalid parameters, it will be reported as an error.
  • Implement SocketInterface. This is necessary so the Proxy class can be defined. This includes the host port address, the port, and command to wait or not for connection. The text editor command must appear like the following:

                public class SocketProxy implements mySockets
                private Socket mySocket;
                private BufferedReader myIn;
                private PrintWriter myOut;
                public SocketProxy( String myHost, int myPort, boolean myWait )

  • Wait for the establishment of a new connection. When the connection is activated, there will be an input stream of BufferedReader that will be opened. This will then be passed to the PrintWriter class’ output stream. This is the one to be used by the server to forward any kind of information. Take the commands below as a guide:

                if (myWait) {
                ServerSocket myServer = new ServerSocket( myPort );
                mySocket = myServer.accept();
                mySocket = new Socket( myHost, myPort );
                myIn = new BufferedReader( new InputStreamReader(
                myOut = new PrintWriter( mySocket.getOutputStream(), true );
                } catch( IOException e ) { e.printStackTrace(); }

  • Read the server's input stream. Using the readLine method, read the input and then return to writeLine method. This must read as follows:

                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 );

  • Close the socket. After the server has finished passing information in between the connections of the main server and the client computer, close or end the network socket. To do this, this command must be followed:

                public void dispose() {
                try {
                } catch( IOException e ) { e.printStackTrace(); }
                } }

Once this process is finished, you have successfully created a proxy server using Java. Now, you can freely and successfully use the server. Even if you can’t see the success physically, the communication between the server and a computer is enough proof of the success of this task.


Share this article!

Follow us!

Find more helpful articles: