Firewall tunneling

Connect to a Java server via HTTP when the client runs behind a proxy/firewall

Q: How do I connect to my Java server with a socket when the client applet/application runs behind a proxy/firewall?

A: I tried it with an applet, but it throws the "host unreachable" exception, among others.

However, there are ways to connect your Java applications to Java servers through HTTP. This is sometimes called firewall tunneling. To do this easily, create servlets on the server side and wrap all client messages in HTTP requests.

On the client side, your code should use the URLConnection to send data to the server:

URL url = new URL("");
URLConnection conn = url.openConnection();
conn.setRequestProperty("Content-Type", "application/octet-stream");
//Open output stream and send some data.
OutputStream out = conn.getOutputStream();
//I am not going to send anything on "out," but you can fill this in.
//Open input stream and read the data back.
InputStream in = conn.getInputStream();
//Here you would read the data back.

On the server side, when the client says openConnection(), the servlet's doPost() method will be called. In that method, you can read the data and write back to the client.

Random Walk Computing is the largest Java/CORBA consulting boutique in New York, focusing on solutions for the financial enterprise. Known for their leading-edge Java expertise, Random Walk consultants publish and speak about Java in some of the most respected forums in the world.