I am trying to get into doing peer-to-peer multiplayer, and to learn I wanted to start with a simple message service; IE a client can set the IP and port and can either host or connect to said address and then send messages to each other.
Currently this works while using 127.0.0.1 (local host address), but to test I wanted to get another machine to try and connect using the actual IP address, after some looking I came across (for Windows)
But this returned my routers address which didn’t work, what I want is to get the address that other machines could use to connect (Same as if you went to a site like https://whatismyipaddress.com) so I can test that my system works across machines, and so in the future when having the player set up a lobby it is usable by others.
How can I get the users current real IP address so they can let others connect to their hosted servers?
It’s not the computer’s IP address you are looking for. You are trying to get the router’s public IP address, computers on the network do not normally have this information, it would be easiest to use an external service like https://whatismyipaddress.com/
However the router also has a firewall/nat that must be traversed, usually this means port forwarding. Other options include UPnP and hole punching.
Late reply as I have worked on this in small amounts on and off since the OG post, but UPNP ended up being what I was looking for, and Ditzy Ninja’s UPNP Tutorial ended up being a massive help, though I ran into an issue after that;
I can host my server and the hosting instance recognises it as connected and even when going to the UPNP section on my router I can see the port is opened, but when trying to connect with the other instance as a client I get an issue.
If I try using (My Ip):(Port) then it fails to find it and times out, if I try to do it using the routers local address it works fine like it did when using local host, but trying to use the public ip with the port fails.
Am I missing something or is this a known beginner issue that can be worked around?
Update; I had been trying TCP first, and even with trying to check the gateway was valid using the built-in functions and checking the UPNP devices on my routers manager, when trying the same thing with UDP it worked just fine. So either my checks are insufficient or my router is weird.[1]
Either way; it now works, Thanks!
For future people finding this; Godot’s UPNP Class is an area to look at for doing peer-to-peer, there are other options that don’t involve either dedicated servers or relay servers like @gertkeno mentioned such as port forwarding or hole-punching.