HostData.useNat

How is HostData.useNat set?

In Unity 3, Network.useNat (though outdated) updates correctly when initializing a server. Performing InitializeServer(connections, listenPort, useNat) using !Network.HavePublicAddress() causes Network.useNat on the server to go from false (default) to true because I don’t have a public address. I register the server with the default master server. So far, so good.

On the same computer, I launch a copy of the same game to investigate as a client. I see my server in the HostData[ ] I poll from the master server, but its HostData.useNat value is false despite the server’s Network.useNat being true.

How is HostData.useNat set? The MasterServer clearly isn’t deriving it from the server’s Network.useNat value. Does it do a another test under the hood and set it to a different value? Does the master server detect that the client is on the same computer as the server, and set useNat to false for the HostData[ ] polled by the client?

I may be missing something, but HostData.useNat seems like the only data a client can use to determine how to use Network.Connect(), whether with a guid (with NAT), or an IP and remotePort (without NAT), so understanding what’s going on would help me greatly. Thanks!

The master server sets the value to whatever it is set when you call InitializeServer(count,port,useNat). But there is a special case which you are observing there, when the master server detects that both server and client are coming from the same network it returns the internal IP(s) and sets HostData.useNat to false. That way using Network.Connect(hostData) or using the values in hostData directly will “just work”.

If it didn’t do this you would have to handle this special case yourself, and when a server is running with NAT enabled then a local client won’t be able to connect to the external IP + outside port.

The bug I notice here is when the 2 clients are on different subnets behind the same external IP. In my test group, this is approximately everyone in a work environment these days.