How to connect locally without IP (NAT Punch and Relay Fallback)

So Im trying to connect from a laptop to the server in other computer (in the same network) but I want the clients to find the server without having to set the IP on the Unity Transport script (in case I want to run the server in other PC or network)
In the documentation it says that I can use the :sparkles:NAT Punch and Relay Fallback​:sparkles: but it says nothing about how to implement it. So maybe one of you precious creatures could help me.

You need to provide clients with an input field where they can enter the IP or a hostname (myserver.mydomain.com). Hostname resolution is a simple C# script that you can google.

Your server will have two IPs: local and public (the one the router gets assigned for the Wan/Internet). Local network computers can only connect to the local IP (at least in my case). Remote clients need to connect to the public IP and the router needs to have port forwarding configured to forward connections to a specific IP and port on the local network. If that computer also has a software firewall enabled the connection needs to be allowed there as well.

Now comes NAT punching: this is usually some external service that tries to make a direct connection but is not guaranteed to succeed, depending on router/network setup. However, when it succeeds you don‘t have to setup port forwarding.

The punchthrough fallback is a Relay server, Unity has such a service and there‘s documentation for that (search for Unity Services and Unity Relay) service). The Relay simply allows both sides to make a connection to the Relay server, acting as a man in the middle. This however increases the ping/RTT but its better than not being able to connect. Relay costs money though, at least Unity‘s.

To find a server by name other than hostname or IP you need a matchmaking service, again Unity provides one.

Personally for testing with friends I just setup a port forward, disable the software firewall (because it is terribly confusing to configure unlike the router and would still block connection attempts). Then I registered a free hostname on noip.com and set that account up on my router so users can find my server via a friendly name and it doesnt matter if my public IP were to change…

Note that the transport Inspector properties for addresses only work with IPs, not hostnames. Like I said: you need some input field for that and resolve hostname to IP if necessary before feeding that into the transport config.

I think this issue will come up more often so I‘m looking into publishing and documenting this particular setup, especially since it comes at no external cost but some effort on the hoster‘s part.

Btw, locally your server PC also has a hostname. You can find that in the windows network config and maybe ipconfig spits that out too. Usually something like „MyComputer.local“. But you‘d still have to apply the hostname to IP resolution script, which you can find on stackoverflow.

Thanks for the info @CodeSmile !
In my case I have a server that provides the network (but this server can be on different devices)
Then I have a bunch of oculus quest 2 whose are the clients.
Both the pc with the server and the oculus are connected to the same router, but I need the oculus to find the server without writing any IP (just to automate the user experience)
I already did this using Mirror networking with the Network discovery (In case you dont know mirror networking → Network discovery is a solution provided by the Mirror plugin that allow you to detect servers in the same network but differents devices)
And I was wondering if there is anything similar with Netcode for gameobjects.

Actually, there is! I just ran across this later today, there’s the Unity for Netcode community extensions which includes NetworkDiscovery.

Thank you so much!! That’s exactly what I was looking for :p:p.

It is quite a complicated situation. But when creating my site, I still managed to do it myself once. First, you must download and install a proxy server that matches your request. Then you need to install a VPN. And make settings for clients. But if that doesn’t work for you, you can find someone like https://apiip.net/. Special applications are also designed to hide clients’ addresses, including yours. The only drawback is that they are hard to set up for public access. But you can try all the options.