本文介绍了如何知道哪个本地应用程序连接到我的套接字 (Windows)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到某个 TCP 端口的 Windows 服务,该端口用于我的应用程序之间的 IPC.

I have a windows services that bind to some TCP port, this port is use for IPC between my application.

是否有编程(WinAPI/WinSocket 等)方法可以知道哪个应用程序连接到我的端口?

Is there a programming (WinAPI/WinSocket and etc) way to know which application connected to my port?

即在我的 Windows 服务中,我想获得连接到我的端口的进程的 PID.

i.e. in my Windows Services I would like to get a PID of the process that connected to my port.

推荐答案

如果您正在寻找与 netstat 相同的 WinAPI 方式.您可能需要以下 API:GetExtendedTcpTable

If you're looking for WinAPI way of doing the same as netstat. You probably want the following API:GetExtendedTcpTable

使用 TCP_TABLE_OWNER_PID_ALL 参数查找结果.

Look for the results with TCP_TABLE_OWNER_PID_ALL argument.

生成的 MIB_TCPTABLE_OWNER_PID 结构有许多 MIB_TCPROW_OWNER_PID 结构具有 dwOwningPid,这是您要查找的进程 ID.

The resulting MIB_TCPTABLE_OWNER_PID structure has many MIB_TCPROW_OWNER_PID structures that has dwOwningPid which is the process ID you are looking for.

这篇关于如何知道哪个本地应用程序连接到我的套接字 (Windows)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:55