检测本机UDP,TCP监听,TCP客户端连接的所有端口


  private bool ISPortUsed(int port)
        {
            IList portUsed = PortUsing();
            return portUsed.Contains(port);
        }

        private IList PortUsing()
        {
            //获取本地计算机的网络连接和通信统计数据的信息 
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();

            //返回本地计算机上的所有Tcp监听程序 
            IPEndPoint[] ipsTCP = ipGlobalProperties.GetActiveTcpListeners();

            //返回本地计算机上的所有UDP监听程序 
            IPEndPoint[] ipsUDP = ipGlobalProperties.GetActiveUdpListeners();

            //返回本地计算机上的Internet协议版本4(IPV4 传输控制协议(TCP)连接的信息。 
            TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();

            List<int> allPorts = new List<int>();
            allPorts.AddRange(ipsTCP.Select(n => n.Port));
            allPorts.AddRange(ipsUDP.Select(n => n.Port));
            allPorts.AddRange(tcpConnInfoArray.Select(n => n.LocalEndPoint.Port));

            return allPorts;
        }

 全部源码下载:https://download.csdn.net/download/hanghangz/11250029


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM