如何检查谁在C#中使用某些端口? [英] How to check who uses certain port in C#?

查看:112
本文介绍了如何检查谁在C#中使用某些端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定正在使用哪个进程/可执行文件,例如本地主机的端口80?我想知道,例如它是否是Apache Server等.

How to determine what process/what executable is using, say, port 80 of my localhost? I would like to know, for example if it is Apache Server, etc.

我们可以从ipProperties.GetActiveTcpListeners()获得一些信息吗?我只看到本地端点,远程端点和状态.

Can we get some information from ipProperties.GetActiveTcpListeners() ? I've only seen local endpoint, remote endpoint and state.

推荐答案

您可以通过管道传输netstat -o的输出并对其进行解析,但这可能是一个充满头痛和极端情况的可怕主意.

You can pipe the output of netstat -o and parse it, but that's probably a terrible idea full of headaches and edge cases.

在后台,netstat -o使用 GetTcpTable2 API方法,该方法返回MIB_TCPTABLE2结构,每个端口均由MIB_TCPROW2结构表示.您必须使用P/Invoke从C#中访问此代码,为表和行构建互操作结构

Behind the scenes, netstat -o uses the GetTcpTable2 API method from the IPHelper library, which returns a MIB_TCPTABLE2 structure, with each port represented by a MIB_TCPROW2 structure. You'll have to use P/Invoke to access this from C#, building interop structs for the table and the row

检查PInvoke.Net,我发现类似的API调用已经映射到C#- GetExtendedTcpTable -列出了应用程序的可用TCP端口.您可以将其用作构建互操作结构和声明的基础.

Checking PInvoke.Net, I see that a similar API call has already been mapped to C# - GetExtendedTcpTable - which lists available TCP ports for an app. You can use that as a base for building your interop structs and declarations.

这篇关于如何检查谁在C#中使用某些端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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