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

Kiril Kirov picture Kiril Kirov · Dec 17, 2010 · Viewed 7k times · Source

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.

Answer

Billy O'Connor picture Billy O'Connor · Dec 17, 2010

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