Skip to content

Commit 4f757a7

Browse files
committed
Potentiall fix to Hostname resolution issue
1 parent 21a1fa4 commit 4f757a7

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportConnectionTests.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,34 @@ public IEnumerator ConnectSingleClient_Hostname()
137137
InitializeTransport(out m_Server, out m_ServerEvents);
138138
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);
139139

140-
// We don't know if localhost will resolve to 127.0.0.1 or ::1, so we wait until we know
141-
// before starting the server. Because localhost is pretty much always defined locally
142-
// it should resolve immediatly and thus waiting one frame should be enough.
140+
// We don't know if localhost will resolve to 127.0.0.1 or ::1 (or even a device LAN
141+
// IP on some Android versions), so we wait until we know before starting the server.
142+
// We poll until GetLocalEndpoint() returns a valid endpoint rather than assuming one
143+
// frame is always enough — resolution may span multiple driver updates on some platforms.
143144

144145
// We'll need to retry connection requests most likely so make this fast.
145146
m_Clients[0].ConnectTimeoutMS = 50;
146147

147148
m_Clients[0].SetConnectionData("localhost", 7777);
148149
m_Clients[0].StartClient();
149150

150-
yield return null;
151+
// Wait until hostname resolution has completed and the driver has bound.
152+
// On some Android devices "localhost" can resolve to the device's LAN IP rather than
153+
// a loopback address, so we use the actual resolved address instead of hardcoding
154+
// "127.0.0.1" or "::1" based solely on the address family.
155+
NetworkEndpoint endpoint;
156+
var resolutionDeadline = Time.realtimeSinceStartup + 2f;
157+
do
158+
{
159+
yield return null;
160+
endpoint = m_Clients[0].GetLocalEndpoint();
161+
} while (endpoint.Family == NetworkFamily.Invalid &&
162+
Time.realtimeSinceStartup < resolutionDeadline);
163+
164+
Assert.AreNotEqual(NetworkFamily.Invalid, endpoint.Family,
165+
"Timed out waiting for localhost hostname resolution to complete.");
151166

152-
var endpoint = m_Clients[0].GetLocalEndpoint();
153-
var ip = endpoint.Family == NetworkFamily.Ipv4 ? "127.0.0.1" : "::1";
154-
m_Server.SetConnectionData(ip, 7777, ip);
167+
m_Server.SetConnectionData(endpoint.Address, 7777, endpoint.Address);
155168
m_Server.StartServer();
156169

157170
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_ClientsEvents[0]);

0 commit comments

Comments
 (0)