This sample code shows how to check if a TCP port is available for listening on the local computer.
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.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; | |
} |
No comments:
Post a Comment