使用 C# 列出已使用的 TCP 端口
这就是我的问题的答案。
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我得到相同的结果:
但它也显示侦听器 (
ipGlobalProperties.GetActiveTcpListeners()
) 可能会也可能不会关闭。使用您的示例(其中有一个额外的
Console.WriteLine
I get the same result:
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这是一个猜测,但 TCPView 可能还显示侦听器 tcp 端口(ipGlobalProperties.GetActiveTcpListeners())
It's a bit of a guess but TCPView probably also shows listener tcp ports (ipGlobalProperties.GetActiveTcpListeners())