Connecting Java sockets over Wi-Fi?
I am trying to create a simple messenger using javafx and the java.net package. Right now I have a client class and a server class that connect using serverocket and a client socket on a random port. It works if I run both on the same computer, or run them on different computers as long as both are connected to the same Wi-Fi and I provide the IP address.
However, this does not work if the two computers are not connected to the same Wi-Fi. How do you make it work? Sorry I am new to java.net and the web.
source to share
You need to traverse the NAT of your router. This can be done in one of the following ways:
If your router supports it, you can use a UPnP library like this one . UPnP basically asks the router to allow talking to the outside network on the port.
However, since not all routers support UPnP, you can try Port Punching Protocol like STUN , there are libraries for java available.
If that doesn't work, you need to pass the network traffic through your own servers with a protocol such as TURN .
There is an ICE protocol that combines the first 2 protocols. In java, this can be done with libraries like ICE4J .
source to share