Windows 上套接字号的 PID?
我需要计算各种应用程序从网络发送和接收的字节数。 首先我考虑使用LSP,但是有很多应用程序不使用LSP全部(SMB 例如)。 这就是为什么我写了一个小嗅探器。 该应用程序在 IP 级别上工作,并使用 recvfrom 收集数据。
所以我有远程主机的地址、本地和远程端口。 这很酷,但我还需要本地套接字所有者的 PID。 那么,有没有一种简单的方法来获取这样的PID呢?
解决方法是使用 GetTcpTable、GetUdpTable 或 AllocateAndGetUdpExTableFromStack 和 AllocateAndGetTcpExTableFromStack(Windows 2000 之间的这些函数有很多差异、Windows XP、Windows XP SP2 和 Windows Vista)并查找结果表,但这似乎不优雅且效率低下......
那么是否有一种“GetPIDOfSocket”函数? 分辨率应为 Win32(不 Win32) org/wiki/.NET_Framework" rel="nofollow noreferrer">.NET) 仅限,因为可以使用各种语言,例如 C++ 和 德尔福。
I need to count amount of bytes sent and received from the network by various applications. First I thought about using LSP, but there is a lot of applications that do not use LSP at all (SMB for example). This is why I have written a small sniffer. This application works on the IP level and collects data using recvfrom.
So I have the address of the remote host, local and remote ports. This is pretty cool, but I also need to have the PID of the local socket owner. So, is there a simple way to obtain such PID?
The workaround is using GetTcpTable, GetUdpTable Or AllocateAndGetUdpExTableFromStack and AllocateAndGetTcpExTableFromStack (there are a lot of differences in those functions between Windows 2000, Windows XP, Windows XP SP2 and Windows Vista) and to lookup result tables, but it seems inelegant and inefficient...
So is there a kind of A "GetPIDOfSocket" function? The resolution should be Win32 (no .NET) only, as various languages can be used, e.g. C++ and Delphi.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
GetTcpTable
或AllocateAndGetTcpExTableFromStack
不是解决方法。 这实际上是其他netstat类型应用程序的工作方式。据我所知,没有任何 Win32
"GetPIDOfSocket"
函数。 您唯一的选择是使用端口表功能进行轮询。 但至少您可以自己编写代码,而不必生成 netstat 进程。请参阅 Sysinternals
netstatp
的 C 源代码 此处。 内容不多,但很值得一看。Using
GetTcpTable
orAllocateAndGetTcpExTableFromStack
is not a workaround. It's actually how other netstat-type applications work.As far as I know, there isn't any Win32
"GetPIDOfSocket"
function. Your only option is to poll using the port table functions. But at least you can code it up yourself and don't have to spawn the netstat process.See the Sysinternals C source code for
netstatp
here. There's not a lot of it and it's well worth a look.