C#檢測遠程計算機端口是否打開

xg48 10年前發布 | 3K 次閱讀 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()
    {
        //code from http://www.sharejs.com/codes

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

 本文由用戶 xg48 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!