8

I have a simple requirement of a software level port forwarding/tunnelling of socket based communication.

  • I have a source server and port using Sockets. This is a java program which works both in windows and linux and this is irrelevant.
  • I have devices which keep sending data to this port. There may be a bi-directional communication
  • I want to redirect this data to another remote server and port. So for the clients they will not have to worry about change of ip address whenever I move my app server.

Are there any tools/deamon/service programs which I can use to configure and do this?

I tried SSH, but to my understanding this needs a SSH protocol enabled server. In my case this is not applicable. I also tried using JSch but this again is an implementation of SSH in java format.

Can someone throw some pointers? Is it possible to use iptables NAT in linux?

Muthu
  • 2,675
  • 4
  • 28
  • 34

3 Answers3

4

You can try netcat or socat (it's more powerful than netcat)

An example for socat to forward port 80 using tcp4:

socat tcp4-listen:80,fork tcp4:{another server}:{another port}

and refer to http://en.wikipedia.org/wiki/Netcat#Port_Forwarding_or_Port_Mapping for netcat

Both are not java-related.

Udo Held
  • 12,314
  • 11
  • 67
  • 93
Tony Wang
  • 193
  • 1
  • 10
  • Wow! How did I forget this!! I have a question though! Can socat or netcat automatically reconnect if a connection is broken? – Muthu Dec 04 '11 at 08:42
  • I don't know if there is a built-in option to reconnect, but it can be client's job to reconnect if anything wrong – Tony Wang Dec 04 '11 at 08:53
  • This says "2011/12/04 15:52:25 socat[4328] E connect(3, AF=2 ipaddress:5000, 16): Address already in use". I tried the command with reuseaddr option also. – Muthu Dec 04 '11 at 10:28
  • 1
    Just so that everybody is benefitted, the command I finally used "socat -d -d -d -d TCP4-LISTEN:9002,fork,reuseaddr TCP4:127.0.0.1:9000" which enables unlimited number of simultaneous connections. Thanks for the answer. – Muthu Dec 04 '11 at 12:44
2

There is a TCP/IP port forwarding utility named portforward available in code.google.com. It is entirely written in Java.

Jomoos
  • 12,823
  • 10
  • 55
  • 92
0

If you're running xinetd on your system already, it provides a simple port forwarding mechanism that might be useful if you're not running IPTables already.

If you are running IPTables, Server Fault has an excellent, short question with a very similar goal. Though I find it a bit terse more detailed documentation is available.

Community
  • 1
  • 1
sarnold
  • 102,305
  • 22
  • 181
  • 238