Things You'll Need:
- Java development environment
-
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();
} -
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 )
{ -
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(); }
} -
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 );
} -
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(); }
} }













