当前位置: 移动技术网 > 科技>操作系统>Linux > Linux中通过Socket文件描述符寻找连接状态介绍

Linux中通过Socket文件描述符寻找连接状态介绍

2018年07月21日  | 移动技术网科技  | 我要评论

proc虚拟文件系统下面有许多数字命名的子目录,这些数字表示系统当前运行的进程号;
其中/proc/n/fd目录下面保存了打开的文件描述符,指向实际文件的一个链接。如下:

复制代码
代码如下:

[root@xxxxxxx_10_1_17_138 song_test]# ll /proc/25465/fd
total 0
lrwx------ 1 root root 64 apr 14 09:36 0 -> /dev/pts/4 (deleted)
lrwx------ 1 root root 64 apr 14 09:36 1 -> /dev/pts/4 (deleted)
lrwx------ 1 root root 64 apr 14 09:36 10 -> socket:[2289128790]
lrwx------ 1 root root 64 apr 14 09:36 100 -> socket:[2305227922]
<span style="color:#ff0000;">lrwx------ 1 root root 64 apr 14 09:36 101 -> socket:[2305224138]</span>
lrwx------ 1 root root 64 apr 14 09:36 102 -> socket:[2305233625]
lrwx------ 1 root root 64 apr 14 09:36 103 -> socket:[2305215571]
lrwx------ 1 root root 64 apr 14 09:36 104 -> socket:[2305243589]
lrwx------ 1 root root 64 apr 14 09:36 105 -> socket:[2305394065]
lrwx------ 1 root root 64 apr 14 09:36 106 -> socket:[2305394002]

我们想查看101 socket文件描述符的链接状态该怎么看呢?聪明的注意到后面有个数字【2305224138】,这个数字又是哪儿来的呢?看客请往下看。

在/proc/net/tcp目录下面保存了所有tcp链接的状态信息。

复制代码
代码如下:

[root@xxxxxxx_10_1_17_138 song_test]# cat /proc/net/tcp
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
0: 8a11010a:7dc8 00000000:0000 0a 00000000:00000000 00:00000000 00000000 0 0 764789417 1 ffff881051dfcb40 99 0 0 10 -1
1: 8a11010a:0369 00000000:0000 0a 00000000:00000000 00:00000000 00000000 0 0 737748331 1 ffff88106af8f7c0 99 0 0 10 -1
51: 8a11010a:faf4 9c01010a:0cea 06 00000000:00000000 03:00000938 00000000 0 0 0 2 ffff8810516c01c0
<span style="color:#ff0000;"> 52: 8a11010a:21cd 0964010a:2227 01 00000000:00000000 00:00000000 00000000 0 0 2305224138 2 ffff8801402f55c0 23 3 30 10 -1 </span>
53: 8a11010a:fb8a 9c01010a:0cea 06 00000000:00000000 03:000012a8 00000000 0 0 0 2 ffff8810516c04c0
54: 8a11010a:73e5 4511010a:0050 06 00000000:00000000 03:00000ea8 00000000 0 0 0 2 ffff88106898a880
55: 8a11010a:89ad f300010a:1f90 08 00000000:00000001 00:00000000 00000000 0 0 2305271480 1 ffff880869b59740 23 3 0 10 -1
187: 8a11010a:0acb 8811010a:1f90 06 00000000:00000000 03:0000028e 00000000 0 0 0 2 ffff881050e9ccc0
188: 8a11010a:fb6c 9c01010a:0cea 06 00000000:00000000 03:000010cb 00000000 0 0 0 2 ffff88104fd8dd80

看上数字【2305224138】没有,就是这儿来的,到此我们可以找出链接的ip、port链接四元组【8a11010a:21cd 0964010a:2227】这个地方是用十六进制保存的,换算成十进制方式【10.1.17.138:8653            10.1.100.9:8743】;

去网络连接状态里面看一下:

复制代码
代码如下:

[root@xxxxxxx_10_1_17_138 song_test]# netstat -ntp
active internet connections (w/o servers)
proto recv-q send-q local address foreign address state pid/program name
tcp 0 0 10.1.17.138:64428 10.1.1.156:3306 time_wait -
tcp 0 0 10.1.17.138:64244 10.1.1.156:3306 time_wait -
<span style="color:#ff0000;">tcp 0 166 10.1.17.138:8653 10.1.100.9:8743 established 25465/./index_searc </span>
tcp 0 0 10.1.17.138:64394 10.1.1.156:3306 time_wait -
tcp 0 0 10.1.17.138:29669 10.1.17.69:80 time_wait -
tcp 0 0 10.1.17.138:46336 10.1.17.68:80 time_wait -
tcp 0 0 ::ffff:10.1.17.138:8080 ::ffff:10.1.17.136:27247 time_wait -

回到开始的问题:101 socket文件描述符代表的是本地【10.1.17.138:8653】到【10.1.100.9:8743】的一条tcp连接!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网