## Wednesday, April 05, 2006

### A Java based TCP/IP port forwarding utility

I was looking for a port forwarding utility that I can use on a Solaris box yesterday. By searching the Internet, I found the portfwd which is a C++ utility. I downloaded to find out it didn't like my 4.x GCC. Figuring it might cost me more time and effort to rebuild an old version of GCC (without root permission), I decide to write a Java version myself so I can reuse it in other OS platforms in future.

I'm done and it is working very well. It listens on a given port and forward all connection to a destination. The port number and destination are specified in command line. I believe this utility is very useful to setup port forwarding in ad hoc basis. I have it in my USB flash drive so I can start forwarding in anywhere that Java goes.

C:\>java -classpath commons-logging.jar;portforward.jar org.enterprisepower.net.portforward.Forwarder 8080 search.yahoo.com:80
Sep 18, 2008 1:11:14 AM org.enterprisepower.net.portforward.Listener <init>
INFO: Ready to accept client connection on 0.0.0.0:8080

Now, launch your browser and point to http://localhost:8080/ you should get the yahoo search page.

Update (9/8/2013): Source code is now at https://github.com/kennethxu/portforward, happy cloning.

Update (4/19/2009): Since I got a lot of request asking for the source code, I decided to have the source available in Google Code. Feel free to get it from the Subversion repository.

Anonymous said...

This is quite a dated post, however, I'm interested in your cross-platform port forwarding utility. If you sent me the jar file I'd be esstatic :)

Anonymous said...

I#m as well interisted in your jar file.. I would really liek to try it out. Loardzero1 (at) hotmail.com

Kenneth Xu said...

Yes, I still have it and using it. But you need to leave an working email address for me to send you. Loardzero1, your hotmail account is bouncing back:

550 Requested action not taken: mailbox unavailable (state 14).

Anonymous said...

Kenneth,

This is exactly what I was looking for. Can you post the source code?

Thanks Marc

Anonymous said...

Just download the java file and you should be good to go

Anonymous said...

it is an amazing program that I was looking for. can u send me the source code so that I can use it in my own project. I want to thank you inadvance for this generosity. Here is my email address: mdrahman.abdur@gmail.com

Anonymous said...

I've downloaded & tried the JAR and it seems well on the way to what I want to do (CIFS over SSL using a loopback adapter). Would you send me the source code? My email address is Bill.Landahl@pobox.com. Thank you.

Anonymous said...

I tried this. It was working on only local ip or localhost.
but when we try to access over the global ip it was not working.

Kenneth Xu said...

Use netstat -an to see if it is listening on IP 0.0.0.0. It does work on all port unless you specified a particualr local IP address to listen on. Most like your firewall is blocking the port. BTW, I assume anybody doing forwarding would be familiar with basic networking.

Gary said...

Gotta love Google, I echo the sentiments of the crowd, this utility is exactly what I was looking for as well. If you would be so kind as to send the source code to gjsoulman@gmail.com I'd be grateful. TIA Kenneth.

Dmytro said...

Great utility just what I was looking for.

Could you please send the source code for it to me also, my email: d.stasyuk@gmail.com

Many thanks,

Anonymous said...

The program work very unstable.
If many clients connect from different IP, the program can stop working. Or if i have 2 of this program on different computers. one link to another. it's not working at all.

Kenneth Xu said...

How many clients that you have used it with? I personally used it with a dozen of them without any problem. This program uses one Java thread for each client. So yes, as I stated in the post, it is for ad hoc purpose and not meant to scale. You should look for commercial product if you intend to support large amount of clients or feel free to enhance the source code.

As to one link to another, it is very interesting, I never tried that before. But I just give it a test and it worked perfectly fine for me. Getting port forwarding to work is not just a program, you need to have solid knowledge of TCP/IP. Especially in the area of firewall, IP routing and etc.

rastrano said...

Great job! Very very usefull for me!

Is there UDP support? I can really use some UDP port forwarding utility in Java.

v3ss said...

Hello Seems very useful! but what i need is a reverse port forwarder

Instead of making it connect to the some host , the client will forward the tcp port to java app and other side will connect to java app

both of them can talk , communicate.

can u make such one? i will pay for it.

Anonymous said...

Thanks for the source. It is great! I know I'm going to use it. Keep the good work! Cheers!

Anonymous said...

Hi Kenneth,
Could you please send me a copy of the source code to antego_allen (at) hotmail.com .
Thanks

Fabiano said...

Great utility!
Works very well.
It seems fast and reliable.

Fabiano said...

Hi, for others to be able to 'embbed' this project inside other java projetcs, the class 'Listener' must be public.
There are some compile warnings about unused private variables and generic types. Are they kept for compatibility with older Java versions?
Regards.

Kenneth Xu said...

Fabiano, this is simply due to me being lazy to go in and revisit/cleanup the code. The code is open source, you are more than welcome to join and enhance it. Let me know if you are interested.

Fabiano said...

Off course i am. And there are just 3 or 4 compiler warnings, and the issue with the Listener class, nothing else. Just let me know how i can commit or how do you preffer i send you the changes.
I´m planning to try this project under high load (about 150 simultaneous connections) in a production environment.

Kenneth Xu said...

try to create a google code account and request to join the project

Nick said...

Hi
I have been using Forwarder java app for a while now on several projects - it is a great utility - fantastic when you need to ssh / tunnel in to a clients environment and need to jump through several different subnets or NATed environments - great work. Thanks for this simple utility.

Nick

Anonymous said...

Hi Kenneth,
I am trying to tunnel all of the network traffic on the host machine running a java applet on it to a remote machine. Can I get the source code of your port forwarding tool, my email is a.masood.113 at gmail.com.

Venkat said...

Hi Kwnneth. I am looking for the Port forwarding utility. Can you please send me the source code. My address is venkatsamanthula@gmail.com

Kenneth Xu said...

@Nick: glad to know its useful. Thanks for you feedback.

@Venkat: link to source code is right in the blog post.

El3cTrO said...

Thanks,

Using this tool I can access a proxy on another network, so I can bypass the normal web proxy and firewalls that blocks facebook etc. I'm FREEEE!

Vincenzo Dentamaro said...

Great done!
I need to integrate it in the SPTP and GridTask project, can you give me the sources or help me? Thanks

Anonymous said...

hi, i am working on video conferencing tool. for this i need port forwarding.
so can u please send me the jar file.

my mail id is omprakash.iiitb(at)gmail.com

Nick said...

**************************************************************
The code is on the net - it is in the google code library

*************************************************************

john jefferson said...

sir kenneth, can u send the link for the java port forwarding source code. can't find it in your google code.
please send me a feedback if you red this comment

Kenneth Xu said...

Anonymous said...

Hello I'm pretty new to this. I'm having some trouble with executing the java command. I saw that one other person is having the same issue, however I was not able to find a solution on how to rectify. when I run the C:\Users\username>java -classpath commons-logging.jar org.eenterprisepower.net.portforward.Fowarder 80 localhost:10000 I get the following error message [code]Exception in thread "main" java.lang.NoClassDefFoundError: org/eenterprisepower/
net/portforward/Fowarder
Caused by: java.lang.ClassNotFoundException: org.eenterprisepower.net.portforwar
d.Fowarder
at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)[/code] I set the classpath and echoed it to ensure that the two jar files were noted, but I continue to get the error message. I'm not sure where I could be going wrong. Any help/guidance would be greatly appreciated.

Kenneth Xu said...

There was a typo in your posted command. It's single e instead of double e. You can also copy and paste from the blog above.

медлительный ресурс said...

Thank you for the sources - they are useful to me!

Nerfbrisbane said...

Hey man, I have been trying to port forward so that I can play some Minecraft Multiplayer with my mates. One of my friends told me about his site and I wonder if you could send me the download, Thanks Mate, If you will, send it to kazukijames (at) hotmail.com, or if that doesnt work jameslingman (at) hotmail.com Thanks

Kenneth Xu said...

@Nerfbrisbane, the download link for both source and binary are available right on the blog post itself.

Anonymous said...

How can I get debug output from this?
Thanks
Wozza

Kenneth Xu said...

@Wozza, it uses commons logging (http://commons.apache.org/logging/), if you know how to config Common Logging, you can control the logging of this. Otherwise, you'll need to read up the doc associated with the version of common logging you use. Here is the simplelog configuration info if you use the one come in the zip: http://commons.apache.org/logging/commons-logging-1.0.4/docs/apidocs/org/apache/commons/logging/impl/SimpleLog.html

Sukumar said...

Hi
It looks great. I am planning to implement similar thing in my code. can you please send the code to me .

Ramiz said...

Hi
i m looking for the jar files that i can import in my android project as external libraries that can forward port of my android supported device.

Anonymous said...

Hi, i am new with java, and just tried to use this jar files, but i got this message:

C:\>java -classpath commons-logging.jar;portforward.jar org.enterprisepower.net.
Error: Could not find or load main class org.enterprisepower.net.portforward.For
warder

I copied portforward.jar and commons-logging.jar to my java folder jdk1.6.0_23

Kenneth Xu said...

@Willy, you need to have both jars in the folder where your command is invoked. In your example, they should be in C:\. Or you can specify the full path:
-classpath path\to\commons-logging.jar;path\to\portforward.jar

Anonymous said...

Great! It's working, thanks for the quick response.

Allen Chee said...

It's great man, working as a charm, thanks!

vipin ahuja said...
This comment has been removed by the author.
Anonymous said...

Thank you... Very useful

Himanshu dhakecha said...
This comment has been removed by the author.
Himanshu dhakecha said...

Getting Error:

D:\jars>java -classpath commons-logging.jar;portforward.jar org.enterprisepower.net.portforward.Forwarder 8080
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at org.enterprisepower.net.portforward.Forwarder.main(Forwarder.java:13)

waiting for replay...

Kenneth Xu said...

@himanshu, You didn't specify the forwarding destination. Try out the given example first and read about the concept of port forwarding. Unfortunately, the utility could have been more dummies friendly.

Rudra Ganesh said...
This comment has been removed by the author.
Rudra Ganesh said...
This comment has been removed by the author.
Rudra Ganesh said...
This comment has been removed by the author.
Unknown said...

is it support source port rang forward then to remote single port ?

Nick said...

No - Listens on 1 port, and forwards to 1 port.
BUT - it does handle multiple connections
AND you can run multiple instants in different port number all pointing to the same port - FUGLY way to to a port range to single port, but can work (i.e I have had 2 instances running on 443 and 80, both redirecting to 8080 on internal server .... )

Nick said...

I am surprised and please this code still lives on and people find this utility useful.
This utility has saved my bacon on more then 1 occasion when I need a reliable, light weight TCP/IP fording solution, that "just works".