Created
April 30, 2015 15:20
-
-
Save segor/1146668314c42b9cf73f to your computer and use it in GitHub Desktop.
How to check if a TCP port is available for listening
This file contains hidden or 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.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