Skip to content

Commit c37403d

Browse files
committed
Fallback too ip4
1 parent 4b2490c commit c37403d

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

dotnet/src/webdriver/Internal/PortUtilities.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// under the License.
1818
// </copyright>
1919

20-
using System;
2120
using System.Net;
2221
using System.Net.Sockets;
2322

@@ -34,20 +33,23 @@ public static class PortUtilities
3433
/// <returns>A random, free port to be listened on.</returns>
3534
public static int FindFreePort()
3635
{
37-
var tcpListener = new TcpListener(IPAddress.IPv6Any, 0);
38-
39-
// Enable dual-mode to also work with IPv4 connections
40-
tcpListener.Server.DualMode = true;
41-
4236
try
4337
{
44-
tcpListener.Start();
45-
46-
return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
38+
var listener = new TcpListener(IPAddress.IPv6Any, 0);
39+
listener.Server.DualMode = true; // Listen on both IPv4 and IPv6
40+
listener.Start();
41+
int port = ((IPEndPoint)listener.LocalEndpoint).Port;
42+
listener.Stop();
43+
return port;
4744
}
48-
finally
45+
catch (SocketException)
4946
{
50-
tcpListener.Stop();
47+
// If IPv6Any is not supported, fallback to IPv4
48+
var listener = new TcpListener(IPAddress.Any, 0);
49+
listener.Start();
50+
int port = ((IPEndPoint)listener.LocalEndpoint).Port;
51+
listener.Stop();
52+
return port;
5153
}
5254
}
5355
}

0 commit comments

Comments
 (0)