Private vs Public IP Addresses

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RedBlueCarrots

I want to try and make a online multiplayer game using godot, as it is the engine I have the most experience in, however I am still confused at how networking works.

Following the documentation for high-level networking through the eNet framework, I was able to establish connections using “127.0.0.1”, then “10.70.207.133”, which is my IPv4 private IP address. However I am aware that this should only work in LAN connections, which is not what my goal is to accomplish.

My public IP address at the moment (220.240.94.XXX) did not work when testing it out with a client and server running on the same computer.

I was curious to see if anyone knows why this is.

:bust_in_silhouette: Reply From: Calinou

As far as I know, this is a general networking issue. You can’t connect to your own public IP address on the local network, even if you’ve forwarded the required port(s).

:bust_in_silhouette: Reply From: Rodrigo Matos

I’m not a networking expert, but I’m kinda experiencing the same problems, since I’m also trying to make a local and online multiplayer game in Godot. Other people, correct me if I’m wrong.

You might wanna check these things:

1º - Port forwarding and firewall permission
2º - Hairpinning

You see, there are so many public IPs to be had with IPv4, specifically 2^32 possible addresses (theoretically). So, there’s something called NAT to help with that: instead of giving public IPs to every device, you just give one to every router, and the routers act as middlemen between your local network and the Internet.

In your first example, you were able to connect to your own computer using your IPv4 private IP address. Since both the source and the destination for the packets (basically data) are located in your local network, the communication has no further problems.

But now, you’re trying to connect to your own computer using the public IP address. The situation is different: your public IP isn’t tied to your computer, it is the router’s public IP. How would it know that you want the data to be sent to your computer, if you’re not explicitly saying it?

That’s where port forwarding becomes useful. You can use it to redirect a communication request from your router’s public IP and port number to your computer’s private IP and port number. That’s something you gotta configure manually, in your router settings, or you can look at UPnP (Universal Plug and Play), which is basically an automated way of doing it.

If you have firewall enabled (probably), you will need to enable the communication for the chosen port too (open the port). I don’t know if this is common, but in my router’s web settings, there’s a page for port forwarding and another one for firewall rules. If I add a port mapping, the firewall rules are automatically added, so I just have to map the port and it’s basically done. You will need to check how your router works.

Lastly, for your example, you are trying to communicate from the local network to the local network, but leaving said network (you are using public IP, so you’re “going” to the Internet). This is a specific situation, and not every router is able to establish that communication out of the box. You will need to check if your router supports hairpinning (NAT loopback). Also, it seems like some routers don’t support it out of the box, but you can still make it work by messing with its settings.

I think the topics I’ve listed might answer why you aren’t able to connect to your own computer using your public IP. For online connections (between different local networks), only the port forwarding and firewall permission will be relevant, the hairpinning is for your specific case.