使用C#列出使用的TCP端口 [英] List used TCP port using C#

查看:134
本文介绍了使用C#列出使用的TCP端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题的答案.

如何在C#中列出绑定/使用的TCP端口.来自 jro

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

原始问题. 这就是我使用C#列出TCP端口的方式.它是我在该论坛中找到的经过修改的代码(确切地忘记了我从哪里获得的.如果您是原始开发人员,请通知我,并在适当的时候注明欠款额.)

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

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

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?

推荐答案

我得到相同的结果:

但是它还会显示可能已关闭或未关闭的侦听器(ipGlobalProperties.GetActiveTcpListeners()).

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

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

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

这篇关于使用C#列出使用的TCP端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆