How to check who uses certain port in C#?

你说的曾经没有我的故事 提交于 2020-01-02 07:47:55

问题


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.

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


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/9110211/how-to-check-who-uses-certain-port-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!