使用 C# 列出已使用的 TCP 端口

发布于 2024-09-28 01:51:00 字数 1469 浏览 2 评论 0原文

这就是我的问题的答案。

如何在 C# 中列出绑定/使用的 TCP 端口。使用 jro< 的修改代码/a>

        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);
        }
    }

原始问题。 这就是我使用 C# 列出 TCP 端口的方式。这是我在这个论坛中找到的修改后的代码(忘记了具体从哪里得到的。如果您是原始开发人员,请通知我,并在适当的地方注明积分。)

    //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);
        }
    }

问题是,结果与 TCPview(协议)中列出的使用的 tcp 端口不同-TCP,本地端口)。 顺便说一句,我确实知道这个列表在调用的确切时间使用了 TCP 端口。 我做错了什么?

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 developer, 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦开始←不甜 2024-10-05 01:51:00

我得到相同的结果:

alt text

但它也显示侦听器 (ipGlobalProperties.GetActiveTcpListeners()) 可能会也可能不会关闭。

使用您的示例(其中有一个额外的 Console.WriteLine

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();
        }
    }
}

I get the same result:

alt text

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();
        }
    }
}
挽清梦 2024-10-05 01:51:00

这是一个猜测,但 TCPView 可能还显示侦听器 tcp 端口(ipGlobalProperties.GetActiveTcpListeners())

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文