aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Port forwarding from inside Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Port forwarding from inside Java?" Watch "Port forwarding from inside Java?" New topic
Author

Port forwarding from inside Java?

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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

Joined: Oct 27, 2005
Posts: 19720
    
  20

I think that should be java.nio.channels.Selector.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Oct 29, 2001
Posts: 8927
    
    9

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
jQuery in Action, 2nd edition
 
subject: Port forwarding from inside Java?