检测端口是否使用

 static bool CheckPortAvailable(int port)
        {
            bool isAvailable = true;
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

            foreach (var tcplisten in ipGlobalProperties.GetActiveTcpListeners())
            {
                if (tcplisten.Port == port)
                {
                    isAvailable = false;
                    break;
                }
            }
            if (isAvailable)
            {
                foreach (var udplisten in ipGlobalProperties.GetActiveUdpListeners())
                {
                    if (udplisten.Port == port)
                    {
                        isAvailable = false;
                        break;
                    }
                }
            }
            return isAvailable;
        }

 

posted @ 2013-10-15 11:19  花生!~~  阅读(329)  评论(0编辑  收藏  举报