Search code examples
c++csocketsunixlsof

How to use lsof(List Opened Files) in a C/C++ application?


Is there any way to get all opened sockets using ? I know the lsof command and this is what I'm looking for, but how to use it in a application?

The idea is to get the FD of an opened socket by its port number and the pid.


Solution

  • Just open the files in /proc/net, like /proc/net/tcp, /proc/net/udp, etc. No need to slog through the lsof sources. :)