开发者

List used TCP port using C#

开发者 https://www.devze.com 2023-01-20 04:14 出处:网络
This is the answer to my questions. How to list binded/used TCP port in C#. Used modified code from jro

This is the answer to my questions.

How to list binded/used TCP port in C#. Used modified code from jro

        static void ListUsedTCPPort(ref ArrayList usedPort)
    {
        IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
        IPEndPoint[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpListeners();
        IEnumerator myEnum = tcpConnInfoArray.GetEnumerator();

        while (myEnum.MoveNext())
        {
            IPEndPoint TCPInfo = (IPEndPoint)myEnum.Current;
            usedPort.Add(TCPInfo.Port);
        }
    }

Original questions. This is how i list TCP port using C#. It is modified code i found in this forum(forgot exactly where i got it. If you are the original deve开发者_如何学编程loper, notify me and ill put credits where due.)

    //List used tcp port
    static void ListUsedTCPPort(ref ArrayList usedPort)
    {
        IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
        TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
        IEnumerator myEnum = tcpConnInfoArray.GetEnumerator();

        while (myEnum.MoveNext())
        {
            TcpConnectionInformation TCPInfo = (TcpConnectionInformation)myEnum.Current;
            usedPort.Add(TCPInfo.LocalEndPoint.Port);
        }
    }

Problem is, the results is different from used tcp port listed in TCPview(Protocol-TCP, Local port). By the way, i do know that this list used TCP port at the EXACT time its called. What did i did wrong?


I get the same result:

List used TCP port using C#

But it does also show listeners (ipGlobalProperties.GetActiveTcpListeners()) which may or may not be closed down.

using your example (with an extra Console.WriteLine in there

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Collections;

namespace ConsoleApplication1 {

    static class Program {
        //List used tcp port
        static void ListAvailableTCPPort(ref ArrayList usedPort) {
            IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
            TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
            IEnumerator myEnum = tcpConnInfoArray.GetEnumerator();

            while (myEnum.MoveNext()) {
                TcpConnectionInformation TCPInfo = (TcpConnectionInformation)myEnum.Current;
                Console.WriteLine("Port {0} {1} {2} ", TCPInfo.LocalEndPoint, TCPInfo.RemoteEndPoint, TCPInfo.State);
                usedPort.Add(TCPInfo.LocalEndPoint.Port);
            }
        }

        public static void Main(){
            ArrayList usedPorts = new ArrayList();
            ListAvailableTCPPort(ref usedPorts);
            Console.ReadKey();
        }
    }
}


It's a bit of a guess but TCPView probably also shows listener tcp ports (ipGlobalProperties.GetActiveTcpListeners())

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号