Take the 2-minute tour ×

I plan on building a few programs in Java that take advantage of the ServerSocket and Socket classes. I have the code built correctly, and I can make connections properly. However, I want to make it as easy on the user as possible when it comes to making connections and servers. So, I need a method to port forward from within Java, so that my programs can build servers on a clients machine and communicate with other machines across the internet, yet the user of the program doesn't need to put in any extra effort. Is there any effective way to perform this without accessing any external programs that would need to be downloaded?

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

You could use this little project.

Note: client machines are usually behind NAT gateways so other machines on the internet can not access them directly.

share|improve this answer
add comment

If the server is behind a NAT or a firewall, and you don't want to use a man-in-the-middle to facilitate the connection, then it's going to be pretty hard. I suggest you look into NAT hole punching, and specifically into some open-source Java implementation like Rodi.

share|improve this answer
This only works for UDP and also requires other side to be changed. –  Peter Knego Nov 12 '10 at 21:20
add comment

What about just piping the incoming connection to the output connection?

If you retrieve both InputStream and OutputStream of both sockets you can use a PipedInputStream and a PipedOutputStream to connect two different sockets and forward the connection. See documentation here and here.

share|improve this answer
You still have to write the code to copy the streams, and when you've done that what do you need the pipes for? –  EJP Nov 13 '10 at 4:25
add comment

Your Answer


