using System.Net; using System.Net.NetworkInformation; namespace Netch.Utils { class PortHelper { /// /// 端口是否被使用 /// /// /// public static bool PortInUse(int port) { return PortInUse(port, PortType.TCP) || PortInUse(port, PortType.UDP); } /// /// 指定类型的端口是否已经被使用了 /// /// 端口号 /// 端口类型 /// public static bool PortInUse(int port, PortType type) { bool flag = false; IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] ipendpoints = null; if (type == PortType.TCP) { ipendpoints = properties.GetActiveTcpListeners(); } else { ipendpoints = properties.GetActiveUdpListeners(); } foreach (IPEndPoint ipendpoint in ipendpoints) { if (ipendpoint.Port == port) { flag = true; break; } } ipendpoints = null; properties = null; return flag; } } /// /// 端口类型 /// enum PortType { TCP, UDP } }