zoukankan      html  css  js  c++  java
  • C#如何检测网络端口连接的状态

    C#如何检测/监控远程连接网络端口的情况(例如:3389端口是否处于监听状态,是否建立了连接等)。

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.NetworkInformation;

    namespace test
    {
        class Program
        {


            static void Main(string[] args)
            {
                GetTcpConnections();
            }

            public static void GetTcpConnections()
            {
                IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
          
                TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
                foreach (TcpConnectionInformation t in connections)
                {
                    Console.Write("Local endpoint: {0} ", t.LocalEndPoint.ToString());
                    Console.Write("Remote endpoint: {0} ", t.RemoteEndPoint.ToString());
                    Console.WriteLine("{0}", t.State);
                }
                Console.WriteLine();
                Console.ReadLine();
            }


        }
    }

    运行结果:

    Local endpoint: 127.0.0.1:1025 Remote endpoint: 127.0.0.1:1026 Established
    Local endpoint: 127.0.0.1:1026 Remote endpoint: 127.0.0.1:1025 Established
    Local endpoint: 127.0.0.1:1028 Remote endpoint: 127.0.0.1:16992 CloseWait
    Local endpoint: 127.0.0.1:1110 Remote endpoint: 127.0.0.1:4900 Established
    Local endpoint: 127.0.0.1:2754 Remote endpoint: 127.0.0.1:1110 CloseWait
    Local endpoint: 127.0.0.1:2762 Remote endpoint: 127.0.0.1:1110 CloseWait
    Local endpoint: 127.0.0.1:2773 Remote endpoint: 127.0.0.1:1110 CloseWait
    Local endpoint: 127.0.0.1:2913 Remote endpoint: 127.0.0.1:1110 CloseWait
    Local endpoint: 127.0.0.1:3014 Remote endpoint: 127.0.0.1:1110 CloseWait
    Local endpoint: 127.0.0.1:3531 Remote endpoint: 127.0.0.1:1110 CloseWait
    Local endpoint: 127.0.0.1:4012 Remote endpoint: 127.0.0.1:1110 CloseWait
    Local endpoint: 127.0.0.1:4900 Remote endpoint: 127.0.0.1:1110 Established

  • 相关阅读:
    如何查看Android SDK源码版本
    迁移 Express 到函数计算
    阿里云安全运营中心:DDoS攻击趁虚而入,通过代理攻击已成常态
    Schedulerx2.0支持应用级别资源管理和任务优先级
    Serverless 解惑——函数计算如何安装字体
    一遇到复杂分析查询就卡顿?MySQL分析实例了解一下
    浅谈企业的数据资产管理
    大咖说备份 | 云,让灾备更简单
    急速上线 Serverless 钉钉机器人“防疫精灵”
    Alibaba Cloud Linux 2 LTS 正式发布,提供更高性能和更多保障
  • 原文地址:https://www.cnblogs.com/emanlee/p/2889612.html
Copyright © 2011-2022 走看看