SoFunction
Updated on 2025-03-08

C# method to detect whether the remote computer port is open

This article describes the method of C# to detect whether the remote computer port is open. Share it for your reference. The specific analysis is as follows:

This C# code is used to detect whether the 3389 port of the remote computer is handled with the open state. Other ports can be set according to actual needs.

using System;
using ;
using ;
using ;
namespace test
{
  class Program
  {
    static void Main(string[] args)
    {
      GetTcpConnections();
    }
    public static void GetTcpConnections()
    {
      IPGlobalProperties properties = ();
      TcpConnectionInformation[] connections = ();
      foreach (TcpConnectionInformation t in connections)
      {
        ("Local endpoint: {0} ", ());
        ("Remote endpoint: {0} ", ());
        ("{0}", );
      }
      ();
      ();
    }
  }
}

The operation results are as follows:

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

I hope this article will be helpful to everyone's C# programming.