1

I am trying to write a simple Java program using a ServerSocket to listen on port 4000 on my server. When a client connects to port 4000, I want the program to "redirect" the client to port 5000, where another unrelated program is currently listening. The client will ideally not be aware of this redirect.

Is there a simple way to achieve this in Java?

dismantle
  • 91
  • 2
  • 6
  • Basically u need port-forwarding. Have a look at Java TCP forwarder package - http://jtcpfwd.sourceforge.net/ – Kishore Dec 07 '15 at 02:25
  • I believe this question already answered. http://stackoverflow.com/questions/8167512/java-socket-port-forwarding http://stackoverflow.com/questions/3954454/fast-implementation-of-a-port-forward-in-java – Peter Jerald Dec 07 '15 at 02:28

1 Answers1

1

You can make a simple proxy server in java, here is one example I found. http://www.java2s.com/Code/Java/Network-Protocol/Asimpleproxyserver.htm Its the same kind of concept the minecraft BugeeCord server used to reroute the traffic to a diffrent ports dynamicaly.

If you're running ubuntu on the destination server a simple way to do this is to use iptables to redirect the port.

If you think this will be a solution to your problem you can look at how to set it up here https://askubuntu.com/questions/444729/redirect-port-80-to-8080-and-make-it-work-on-local-machine

Community
  • 1
  • 1
  • Thanks. In your first solution, all data between the client and the program at port 5000 will pass through the proxy, right? Is there a way to have it so the client then communicates with the program at port 5000 directly? My server is running Ubuntu, but I'm not sure if the iptables method will work, since if I route port 4000 to 5000, and my Java program listens on 4000, it will never receive a connection? – dismantle Dec 07 '15 at 03:50