码迷,mamicode.com
首页 > 其他好文 > 详细

查看哪些端口被使用

时间:2014-11-16 22:47:44      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   ar   os   使用   sp   for   

/// <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

查看哪些端口被使用

标签:style   blog   http   color   ar   os   使用   sp   for   

原文地址:http://www.cnblogs.com/z5337/p/4102481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!