2010-10-18 77 views
1

这是我的问题的答案。使用C#列表使用TCP端口#

如何在C#中列出绑定/使用的TCP端口。二手修改后的代码从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端口。这是修改后的代码我在这个论坛中发现(确切忘在那里我得到了它。如果你是原开发商,通知我和虐待放学分,其中因。)

//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端口(Protocol-TCP,本地端口)。顺便说一下,我知道这个列表在它被调用的EXACT时间使用了TCP端口。 我做错了什么?

+1

为什么的功能名称是当它返回所使用的端口“ListAvailableTCPPort”?,并提供从两者的两种不同的列表,将有助于发现问题

来源 – Xaqron 2010-10-18 02:50:27

+0

你说得对,我应该把它命名为ListUsedTCPPort,当我写入它的时候,我必须记住一些东西,现在重命名它 – 2010-10-18 05:30:32

回答

3

我得到了相同的结果:

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

谢谢。现在我知道我的问题是什么。我真正需要的是查看端口号是否当前连接。上面的方法没有列出当前未连接的端口号。 – 2010-10-18 05:39:19

1

这是一个有点猜测,但大概的TCPView还显示监听TCP端口(ipGlobalProperties.GetActiveTcpListeners())

+0

我可以只使用GetActiveTcpListeners()吗?我的意思是,它也获得主动/连接的tcp我的目标是在不知道要使用哪个端口的情况下成功创建服务器。 – 2010-10-18 06:48:30

+0

既不使用GetActiveTcpListeners()也不使用GetActi veTcpConnections()是一个好主意 - 另一个进程可以在您检查端口是否可用以及尝试使用端口之间获取端口。如http://stackoverflow.com/questions/138043/find-the-next-tcp-port-in-net所述,您可以开始监听端口号0,系统将为您分配未使用的端口。另一种方法是从一个随机端口号开始,并尝试继续打开它直到成功(如果失败,则转到下一个数字)。 – 2010-10-19 05:03:42