C#

[C#] 로컬 포트 정보 캐기 + Process ID정보까지

쑤기c 2018. 11. 8. 14:19

포트상태 감시자 만드는 중 간단히 Listen포트정보와 Tcp연결포트 정보를 가져오는 API를 발견하여 정리해둔다.
단, IPv4정보만 출력된다.
netstat 명령어와 같이 모든 정보를 얻고 싶으면 아래 링크의 샘플코드를 참조할 것▼
C# Sample to list all the active TCP and UDP connections using Windows Form appl | 2016-01-20

using System.Net.NetworkInformation; //IPAddress, IPGlobalProperties
using System.Net;//IPEndPoint

static void Main(string[] args)
{
    _Print("IPAddress.Any = " + IPAddress.Any.ToString());
    _Print("IPAddress.None = " + IPAddress.None.ToString());
    _Print("IPAddress.Broadcast = " + IPAddress.Broadcast.ToString());
    _Print("IPAddress.Loopback = " + IPAddress.Loopback.ToString());

    _Print("IPAddress.IPv6Any = " + IPAddress.IPv6Any.ToString());
    _Print("IPAddress.IPv6Loopback = " + IPAddress.IPv6Loopback.ToString());
    _Print("IPAddress.IPv6None = " + IPAddress.IPv6None.ToString());

    Console.WriteLine("\r\npress any key to exit..");
    Console.ReadKey();

    PortList();

    Console.WriteLine("\r\npress any key to exit..");
    Console.ReadKey();
}
public static void PortList()
{
    IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
    IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
    _Print("▼ GetActiveTcpListeners ----------------");
    foreach (IPEndPoint endPoint in ipEndPoints)
    {
        _Print(string.Format("AddressFamily={0}, IP={1}, PORT={2}", endPoint.AddressFamily.ToString(), endPoint.Address.ToString(), endPoint.Port));
    }

    TcpConnectionInformation[] tcpConnInfoArray = ipProperties.GetActiveTcpConnections();

    _Print("▼ GetActiveTcpConnections ----------------");
    _Print("Local\t\t\t ┃Remote\t\t\t ┃State");
    foreach (TcpConnectionInformation tcpConnInfo in tcpConnInfoArray)
    {
        TcpState tcpState = tcpConnInfo.State;
        IPEndPoint localEndPoint = tcpConnInfo.LocalEndPoint;
        IPEndPoint remotrEndPoint = tcpConnInfo.RemoteEndPoint;

        _Print(string.Format("{0}:{1}\t ┃{2}:{3}\t ┃{4}",
            tcpConnInfo.LocalEndPoint.Address, tcpConnInfo.LocalEndPoint.Port,
            tcpConnInfo.RemoteEndPoint.Address, tcpConnInfo.RemoteEndPoint.Port,
            tcpConnInfo.State.ToString()));
    }
}

static public void _Print(string aMsg)
{
    Console.WriteLine(aMsg);
    System.Diagnostics.Debug.WriteLine(aMsg);
}

 

▼출력 결과
IPAddress.Any = 0.0.0.0
IPAddress.None = 255.255.255.255
IPAddress.Broadcast = 255.255.255.255
IPAddress.Loopback = 127.0.0.1
IPAddress.IPv6Any = ::
IPAddress.IPv6Loopback = ::1
IPAddress.IPv6None = ::

▼ GetActiveTcpListeners ----------------
AddressFamily=InterNetwork, IP=0.0.0.0, PORT=80
AddressFamily=InterNetwork, IP=0.0.0.0, PORT=135
AddressFamily=InterNetwork, IP=0.0.0.0, PORT=623
AddressFamily=InterNetwork, IP=0.0.0.0, PORT=1025
AddressFamily=InterNetwork, IP=0.0.0.0, PORT=5357
AddressFamily=InterNetwork, IP=0.0.0.0, PORT=10004
AddressFamily=InterNetwork, IP=0.0.0.0, PORT=31234
AddressFamily=InterNetwork, IP=0.0.0.0, PORT=55920
AddressFamily=InterNetwork, IP=14.9.217.129, PORT=139
AddressFamily=InterNetwork, IP=127.0.0.1, PORT=1065
AddressFamily=InterNetwork, IP=127.0.0.1, PORT=16105
AddressFamily=InterNetwork, IP=127.0.0.1, PORT=55501

▼ GetActiveTcpConnections ----------------
Local		 	 ┃Remote				 ┃State
14.9.217.129:1045	 ┃12.4.11.221:9992	 	┃Established
14.9.217.129:1049	 ┃12.4.11.221:9991	 	┃Established
14.9.217.129:2029	 ┃64.233.188.188:5228	 ┃Established
14.9.217.129:23449	 ┃211.115.106.205:80	 ┃CloseWait
14.9.217.129:24345	 ┃104.17.129.217:443	 ┃Established
14.9.217.129:24675	 ┃121.156.118.102:80	 ┃CloseWait
127.0.0.1:24836	 ┃127.0.0.1:9229		 ┃SynSent
127.0.0.1:24837	 ┃127.0.0.1:9229		 ┃SynSent


해당포트의 Process ID값까지 필요하다면 - 예를 들어 kill process로 포트를 닫아야할 때 - 아래 샘플 소스 사용가능

.NET Framework 4.0 이상 / iphlpapi.dll 활용

C# Sample to list all the active TCP and UDP connections using Windows Form appl




반응형