查看哪些端口被使用

/// <summary>
/// 检测端口是否被占用
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
public bool PortCheck(int port)
{
    bool flag = false;

    IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
    IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();//获取所有的监听连接

    foreach (IPEndPoint endPoint in ipEndPoints)
    {
        System.Diagnostics.Debug.WriteLine(endPoint.Port);
        if (endPoint.Port == port)
        {
            flag = true;
        }
    }

    return flag;
}

也可以 netstat -a 查看当前活动端口

 

转载自 : http://www.cnblogs.com/bayonetxxx/archive/2009/07/22/1498747.html

posted on 2014-11-16 21:44  z5337  阅读(501)  评论(1编辑  收藏  举报