Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of open sockets in Linux using C?

Is there a way to get a list of all open sockets ( socket address or socket descriptor ) in Linux using C in user-space or kernel?

Thank you

like image 602
Misha M Avatar asked May 08 '09 20:05

Misha M


2 Answers

Open and read the following:

/proc/net/tcp - a list of open TCP sockets

/proc/net/udp - a list of open UDP sockets

/proc/net/raw - a list all the "raw" sockets

These are like "regular" files that you open and read with a filehandle and will give you all the information you could possibly need about each socket.

like image 134
Shane C. Mason Avatar answered Sep 28 '22 07:09

Shane C. Mason


This program may be useful for you and demonstrates how to parse the /net/proc/* files sockstat.c

like image 37
nmuntz Avatar answered Sep 28 '22 08:09

nmuntz