File tree Expand file tree Collapse file tree 1 file changed +13
-11
lines changed
dotnet/src/webdriver/Internal Expand file tree Collapse file tree 1 file changed +13
-11
lines changed Original file line number Diff line number Diff line change 1717// under the License.
1818// </copyright>
1919
20- using System ;
2120using System . Net ;
2221using 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}
You can’t perform that action at this time.
0 commit comments