C#判断端口是否被占用

    技术2022-07-11  89

    public static bool PortInUse(int port) { bool inUse = false; IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();//IP端口 foreach (IPEndPoint endPoint in ipEndPoints) { if (endPoint.Port == port) { inUse = true; return inUse; } } ipEndPoints = ipProperties.GetActiveUdpListeners();//UDP端口 foreach (IPEndPoint endPoint in ipEndPoints) { if (endPoint.Port == port) { inUse = true; return inUse; } } return inUse; }

     

    Processed: 0.011, SQL: 9