检查在 linux 操作系统中打开的所有套接字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17523389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:11:11  来源:igfitidea点击:

check all socket opened in linux OS

clinux

提问by stack_A

My program opens a socket with this function:

我的程序用这个函数打开一个套接字:

sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)

sockfd = 套接字(AF_INET、SOCK_RAW、IPPROTO_ICMP)

After finish sending data the socket is closed:

完成发送数据后,套接字关闭:

close(sockfd);

关闭(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 ?

如何检查 Linux 操作系统下打开的所有套接字?

采纳答案by Chankey Pathak

/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

/proc/net/tcp - 打开的 tcp 套接字列表

/proc/net/udp - 打开的 udp 套接字列表

/proc/net/raw -a 列出所有“原始”套接字

These are the files, use catcommand to view them. For example:

这些是文件,使用cat命令查看它们。例如:

cat /proc/net/tcp

cat /proc/net/tcp

You can also use the lsofcommand.

您也可以使用该lsof命令。

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.

lsof 是一个命令,意思是“列出打开的文件”,在许多类 Unix 系统中使用它来报告所有打开文件的列表以及打开它们的进程。

回答by Nithin Bhaskar

You can use netstatcommand

您可以使用netstat命令

netstat --listen

netstat --listen

To display open ports and established TCP connections,

要显示打开的端口和已建立的 TCP 连接,

netstat -vatn

netstat -vatn

To display only open UDP ports try the following command:

要仅显示打开的 UDP 端口,请尝试以下命令:

netstat -vaun

netstat -vaun

回答by Sasha Zezulinsky

Also you can use ssutility to dump sockets statistics.

您也可以使用ss实用程序来转储套接字统计信息。

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:

显示 UDP 套接字:

ss -u -a

To display TCP sockets:

显示 TCP 套接字:

ss -t -a

Here you can read ss man: ss

在这里你可以读到 ss man: ss