How to know which Local Application Connected to my socket (Windows)

元气小坏坏 提交于 2019-12-10 10:54:47

问题


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

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

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


回答1:


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

Look for the results with TCP_TABLE_OWNER_PID_ALL argument.

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.




回答2:


If you mean what process is using (listening on or connected using) your ports, use the following command:

netstat -a -b -o -n

-a will show you all connection (even if they in LISTENING state)

-b will show you the application executable that uses that port

-o will show you the PID of the application

-n will not do dns translations (you probably don't need these for knowing about the application), not necessary



来源:https://stackoverflow.com/questions/826076/how-to-know-which-local-application-connected-to-my-socket-windows

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