开发者

Port Forwarding From Within Java

开发者 https://www.devze.com 2023-01-25 09:05 出处:网络
I plan on building a few programs in Java that take advantage of the ServerSocket and Socket classes. I have the code built correctly, and I can make connection开发者_StackOverflow社区s properly. Howe

I plan on building a few programs in Java that take advantage of the ServerSocket and Socket classes. I have the code built correctly, and I can make connection开发者_StackOverflow社区s properly. However, I want to make it as easy on the user as possible when it comes to making connections and servers. So, I need a method to port forward from within Java, so that my programs can build servers on a clients machine and communicate with other machines across the internet, yet the user of the program doesn't need to put in any extra effort. Is there any effective way to perform this without accessing any external programs that would need to be downloaded?


You could use this little project.

Note: client machines are usually behind NAT gateways so other machines on the internet can not access them directly.


What about just piping the incoming connection to the output connection?

If you retrieve both InputStream and OutputStream of both sockets you can use a PipedInputStream and a PipedOutputStream to connect two different sockets and forward the connection. See documentation here and here.


If the server is behind a NAT or a firewall, and you don't want to use a man-in-the-middle to facilitate the connection, then it's going to be pretty hard. I suggest you look into NAT hole punching, and specifically into some open-source Java implementation like Rodi.

0

精彩评论

暂无评论...
验证码 换一张
取 消