[c] check all socket opened in linux OS

My program opens a socket with this function:

sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)

After finish sending data the socket is closed:

close(sockfd);

But the issue is when the program doesn't run well and is blocking. Thereby the socket will not be closed.

How can I check all sockets opened under Linux OS ?

This question is related to c linux

The answer is


Also you can use ss utility to dump sockets statistics.

To dump summary:

ss -s

Total: 91 (kernel 0)
TCP:   18 (estab 11, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 0

Transport Total     IP        IPv6
*         0         -         -        
RAW       0         0         0        
UDP       4         2         2        
TCP       18        16        2        
INET      22        18        4        
FRAG      0         0         0

To display all sockets:

ss -a

To display UDP sockets:

ss -u -a

To display TCP sockets:

ss -t -a

Here you can read ss man: ss


You can use netstat command

netstat --listen

To display open ports and established TCP connections,

netstat -vatn

To display only open UDP ports try the following command:

netstat -vaun