zoukankan      html  css  js  c++  java
  • c#判断指定端口号是否被占用

    本文系原创

    分享判断指定端口号是否被占用的两种方法:

            /// <summary>
            /// 判断指定端口号是否被占用
            /// </summary>
            /// <param name="port"></param>
            /// <returns></returns>
            internal static Boolean IsPortOccupedFun1(Int32 port)
            {
                Boolean result = false;
                try
                {
                    Process p = new Process();
                    p.StartInfo = new ProcessStartInfo("netstat", "-an");
                    p.StartInfo.CreateNoWindow = true;
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.Start();
                    String output = p.StandardOutput.ReadToEnd().ToLower();
                    string ip1 = "127.0.0.1";
                    string ip2 = "0.0.0.0";
                    System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
                    List<string> ipList = new List<string>();
                    ipList.Add(ip1);
                    ipList.Add(ip2);
                    for (int i = 0; i < addressList.Length; i++)
                    {
                        ipList.Add(addressList[i].ToString());
                    }
    
                    
                    for (int i = 0; i < ipList.Count; i++)
                    {
                        if (output.IndexOf("tcp " + ipList[i] + ":" + port.ToString()) >= 0)
                        {
                            result = true;
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Application.ActiveApplication.Output.Output(ex);
                }
                return result;
            }    
            /// <summary>
            /// 判断指定端口号是否被占用
            /// </summary>
            /// <param name="port"></param>
            /// <returns></returns>
            internal static Boolean IsPortOccupedFun2(Int32 port)
            {
                Boolean result = false;
                try
                {
                    System.Net.NetworkInformation.IPGlobalProperties iproperties = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
                    System.Net.IPEndPoint[] ipEndPoints = iproperties.GetActiveTcpListeners();
                    foreach (var item in ipEndPoints)
                    {
                        if (item.Port == port)
                        {
                            result = true;
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Application.ActiveApplication.Output.Output(ex);
                }
                return result;
            }
  • 相关阅读:
    vs2017中信号与槽连接
    生成格雷码
    结构光三维测量技术
    格雷码生成算法
    结构光三维重建
    Qt之CMake和MinGW编译OpenCV
    qt+opencv编译环境的配置
    vs2017+opencv配置参考链接
    2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)
    2019-3-22c# TextBox只允许输入数字,禁用右键粘贴,允许Ctrl+v粘贴数字
  • 原文地址:https://www.cnblogs.com/mohanchen/p/9042229.html
Copyright © 2011-2022 走看看