Pergunta

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 connections 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?

Foi útil?

Solução

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.

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top