How to find out which local application is connected to my socket (Windows)

I have windows services that are bound to some TCP port, this port is used for IPC between my application.

Is there a programming way (WinAPI / WinSocket, etc.) to find out which application is connected to my port?

i.e. in my windows services, I would like to get the PID of the process associated with my port.

+2
source share
2 answers

If you are looking for a WinAPI way to do the same thing as netstat . You probably need the following API: GetExtendedTcpTable

Look for results with the argument TCP_TABLE_OWNER_PID_ALL .

There are many MIB_TCPROW_OWNER_PID structures in the resulting MIB_TCPTABLE_OWNER_PID structure, which dwOwningPid has the identifier of the process you are looking for.

+3
source

If you mean which process uses (listening or connecting with) your ports, use the following command:

netstat -a -b -o -n

-a will show you all the connections (even if they are in the LISTENING state)

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

-o will show you the application PID

-n will not perform dns translations (you probably do not need this knowledge for the application), not necessarily

0
source

Source: https://habr.com/ru/post/1389331/


All Articles