Skip to content

Commit

Permalink
Revert PortUtil.cs changes (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored May 29, 2018
1 parent 2fcfda4 commit 2b498f4
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/WireMock.Net/Http/PortUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace WireMock.Http
/// </summary>
public static class PortUtil
{
private static readonly IPEndPoint DefaultLoopbackEndpoint = new IPEndPoint(IPAddress.Loopback, port: 0);
private static readonly Regex UrlDetailsRegex = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>\d+)?/", RegexOptions.Compiled);

/// <summary>
Expand All @@ -18,10 +17,17 @@ public static class PortUtil
/// <remarks>see http://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net.</remarks>
public static int FindFreeTcpPort()
{
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
TcpListener tcpListener = null;
try
{
socket.Bind(DefaultLoopbackEndpoint);
return ((IPEndPoint)socket.LocalEndPoint).Port;
tcpListener = new TcpListener(IPAddress.Loopback, 0);
tcpListener.Start();

return ((IPEndPoint)tcpListener.LocalEndpoint).Port;
}
finally
{
tcpListener?.Stop();
}
}

Expand Down

0 comments on commit 2b498f4

Please sign in to comment.