Created
May 28, 2017 19:02
-
-
Save alexander-williamson/3e4e770db51dda6c991d7faaca964618 to your computer and use it in GitHub Desktop.
Get free TCP Ports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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