Skip to content

Instantly share code, notes, and snippets.

@alexander-williamson
Created May 28, 2017 19:02
Show Gist options
  • Save alexander-williamson/3e4e770db51dda6c991d7faaca964618 to your computer and use it in GitHub Desktop.
Save alexander-williamson/3e4e770db51dda6c991d7faaca964618 to your computer and use it in GitHub Desktop.
Get free TCP Ports
using System.Net;
using System.Net.Sockets;
// taken from https://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net
public static class TcpPorts
{
public static int GetFreeTcpPort()
{
var l = new TcpListener(IPAddress.Loopback, 0);
l.Start();
var port = ((IPEndPoint) l.LocalEndpoint).Port;
l.Stop();
return port;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment