Skip to content

Commit d7ff72e

Browse files
committed
Use TcpListener?
1 parent 9788887 commit d7ff72e

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

dotnet/src/webdriver/Internal/PortUtilities.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ public static class PortUtilities
3737
/// </exception>
3838
public static int FindFreePort()
3939
{
40-
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
40+
var tcpListener = new TcpListener(IPAddress.IPv6Any, 0);
4141

4242
try
4343
{
44-
socket.Bind(new IPEndPoint(IPAddress.Any, 0));
44+
tcpListener.Start();
4545

46-
return ((IPEndPoint)socket.LocalEndPoint!).Port;
46+
return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
4747
}
48-
catch (SocketException ex)
48+
finally
4949
{
50-
throw new InvalidOperationException("Unable to find a free port.", ex);
50+
tcpListener.Stop();
5151
}
5252
}
5353
}

0 commit comments

Comments
 (0)