Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Port forwarding from inside Java?

 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to write code using pure Java to port-forward from your router. The PS3 and other programs do it. I would like to know how to do this in Java. What I mean is, without setting up your router, how would you set up port-forwarding so your program could become a server. I am sorry if this is really bad formatting of the question. I don't know how to phrase it. I just want to know how to do port forwarding so a program can be a server using pure Java code.

Thanks,
John Price
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Port forwarding itself is simple enough; you open a java.net.ServerSocket on the "from" port to listen on, and every byte you receive there, you write to a java.net.Socket opened to wherever the data is supposed to go; you'll need to send return data back via the opposite path. You'll either need to dedicate a couple of threads, or use NIO java.nio.channels.Selectors .

But what do you mean exactly, "so a program can be a server?" That's the part I don't understand.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I don't open a port on my server, I haven't been able to successfully use a server program as a server (the router blocks it I assume). Your "Selectors" link leads to a 404 error page.

John Price
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that should be java.nio.channels.Selector.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed my links, sorry.

Port forwarding can only route traffic through open ports; it can't make a path through a firewall if there is no path available to use. Basically all you can do with port forwarding is run a server that listens on port A on machine X, and a client on machine Y that connects to port B on either machine X or Y. The port forwarding connects that client to that server. But either port A or port B must be open on any firewall protecting machine X, or this can't be made to work, and the precise technique for doing it depends on which of those ports is available.
 
Joe Ess
Bartender
Posts: 9300
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are looking for is called Universal Plug and Play. It's a protocol that allows a networked device to control other network devices. In this case, your PS3 tells your router what routes it needs. Sorry, but I haven't used it myself. There seems to be at least one project that provides an API.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic