Find PID of process using a TCP port programmatically on macOS

263 Views Asked by At

Currently I am coding a monitoring tool for macOS. Currently stuck on a problem where I am unable to get the PID of the process who is using the specific port.

for example: Assume a process say a chat server running hosted on port 9999 is currently running and its PID is 12345. I wanted to write a C / C++ / Objective C function which will return 12345 when Input given to that function is 9999.

I know that it is possible using utilities like lsof and netstat. I cannot use this approach as it is not performance efficient. I was hoping that there would some Sysctl MIB which I can use to query this information.

Please help.

Thanks & Regards, Prasanna

0

There are 0 best solutions below