Skip to content

Instantly share code, notes, and snippets.

@segor
Created April 30, 2015 15:20
Show Gist options
  • Save segor/1146668314c42b9cf73f to your computer and use it in GitHub Desktop.
Save segor/1146668314c42b9cf73f to your computer and use it in GitHub Desktop.
How to check if a TCP port is available for listening
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
public bool TcpPortIsAvailableForListening(int port)
{
IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] tcpEndPointArray = ipGlobalProperties.GetActiveTcpListeners();
var isUnavailable = tcpEndPointArray.Any(endPoint => endPoint.Port == port);
return !isUnavailable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment