Search code examples
clinuxnetwork-programmingportnetstat

Get used ports and states


How can I get used ports and their states on Linux? Basically, everything that netstat can do, but in C?


Solution

  • Running strace on a run of netstat will show you the system calls it makes and their arguments.

    $ strace netstat
    ...
    open("/proc/net/tcp6", O_RDONLY)        = 3
    open("/proc/net/udp", O_RDONLY)         = 3
    ...
    

    This is often a good way to find out what a program is doing or the calls it makes and can sometimes be easier than looking at the source if all you need is to find out which call to look up on a man page.