Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check all socket opened in linux OS

Tags:

c

linux

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 ?

like image 643
stack_A Avatar asked Jul 08 '13 09:07

stack_A


People also ask

How do I see open sockets?

One of the simplest ways to check for open ports is to use NetStat.exe. You can find this tool in the System32 folder on Windows 10. With NetStat, you can see open ports or ports that a specific host uses.

How many sockets open Linux?

For most socket interfaces, the maximum number of sockets allowed per each connection between an application and the TCP/IP sockets interface is 65535.

How do I find socket files in Linux?

If you want to search for symbolic links when -L is in effect, use -xtype. s socket D door (Solaris) To search for more than one type at once, you can supply the combined list of type letters separated by a comma `,' (GNU extension). it worked with find .


3 Answers

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

like image 146
Саша Зезюлинский Avatar answered Oct 08 '22 22:10

Саша Зезюлинский


/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 the files, use cat command to view them. For example:

cat /proc/net/tcp

You can also use the lsof command.

lsof is a command meaning "list open files", which is used in many Unix-like systems to report a list of all open files and the processes that opened them.

like image 47
Chankey Pathak Avatar answered Oct 08 '22 22:10

Chankey Pathak


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

like image 25
Nithin Bhaskar Avatar answered Oct 08 '22 20:10

Nithin Bhaskar