Greetings !
I am currently working on a multiplayer game where all players connect to a central Master Server which, among other things, references all currently open matches it knows about so that players can join matches easily and hosted by people they don’t have a way to communicate with.
I have two problems.
- One of these is that I only have one computer, meaning I have to test the Master Server and the client on the same machine, meaning the data Unity keeps about connection IP is local network IP, or even 127.0.0.1 if I connect with that IP from the client. It makes sense of course, no point looking for the public IP when you can have the machine’s IP on your network directly. But this means the IP that is shown to other players who are not on the same network is the local IP relative to the Master Server, meaning it won’t work for them.
I figured being able to get the local machine’s public IP and sending it to the Master Server separately no matter what could be useful. Problem is, I’m not sure how I could achieve the “get the local machine’s public IP” part reliably. Of course that problem alone would only apply for players logging from the same machine the Master Server is hosted on, but it would sure be annoying for all the tests I have to run.
- The other problem I have is NAT punching. The Master Server will have its NAT configured properly of course, but since the matches themselves work on a peer to peer model, I’d need everyone wishing to host a match to open the right port on their router, which is probably way too much to ask from the average player.
As such, I’m looking for a way to build a NAT punch through algorithm within Unity. I’ve googled it of course, and found interesting information, but nothing that really helped me fully understand or solve this problem.
Now, what I do understand is that public IP is the router’s adress, not the computer’s, meaning the router need to know where to redirect information coming from a given port, and that’s what NAT is for. Knowing that, I figured I could try to cobble something where clients would send their public AND local network IP to the Master Server no matter what, so then when a connection needs to be established there would be a way to “tell” the router at the public IP adress which machine we’d like to be connected to.
Thing is, as much as I don’t have a clue how to get a machine’s public IP reliably, I also don’t know how to get a machine’s local IP reliably, and even if I could I don’t think there would be a way to put it to use with Unity’s API.
If you guys need more detail on what I know, what I need, what I already have… just ask, I’ll be keeping an eye on this thread all day every day, since it’s critical to my current student project.
Thanks !
PS : I’ve seen a few things like this NAT Traversal - Automatic port forwarding, punch-through, and more! - Community Showcases - Unity Discussions but it’s all Unet, which is the high level API we’re not using. We might be shooting ourselves in the foot on some aspects but since it’s a student project we figured the most educative way would be the best way, even if it takes slightly more time to fully comprehend.