tcp的连接建立:
在本机执行如下命令:
sudo tcpdump -S port 8080 -vv -n -i lo0
tcpdump: listening on lo0, link-type NULL (BSD loopback), capture size 262144 bytes
00:35:50.265450 IP (tos 0x10, ttl 64, id 35702, offset 0, flags [DF], proto TCP (6), length 64, bad cksum 0 (->b12f)!)
127.0.0.1.57273 > 127.0.0.1.8080: Flags [S], cksum 0xfe34 (incorrect -> 0x3271), seq 84347051, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 1525310591 ecr 0,sackOK,eol], length 0
00:35:50.265585 IP (tos 0x0, ttl 64, id 10086, offset 0, flags [DF], proto TCP (6), length 64, bad cksum 0 (->1550)!)
127.0.0.1.8080 > 127.0.0.1.57273: Flags [S.], cksum 0xfe34 (incorrect -> 0xa903), seq 2958367133, ack 84347052, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 1525310591 ecr 1525310591,sackOK,eol], length 0
00:35:50.265599 IP (tos 0x10, ttl 64, id 54858, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->6667)!)
127.0.0.1.57273 > 127.0.0.1.8080: Flags [.], cksum 0xfe28 (incorrect -> 0xf11f), seq 84347052, ack 2958367134, win 12759, options [nop,nop,TS val 1525310591 ecr 1525310591], length 0
00:35:50.265614 IP (tos 0x0, ttl 64, id 63250, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->45af)!)
127.0.0.1.8080 > 127.0.0.1.57273: Flags [.], cksum 0xfe28 (incorrect -> 0xf11f), seq 2958367134, ack 84347052, win 12759, options [nop,nop,TS val 1525310591 ecr 1525310591], length 0
下面说明下这里的标志代表tcp首部中的6个标志比特中的4个。如下图所示:
标志 | 3个字符缩写 | 描述 |
S | SYN | 同步序号 |
F | FIN | 发送方完成数据发送 |
R | RST | 复位连接 |
P | PSH | 尽快能地将数据送往接收进程 |
. | 以上4个标志均设置为0 |
当进行关闭连接的时候,输出如下:
00:56:40.220526 IP (tos 0x10, ttl 64, id 33599, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->b972)!)
127.0.0.1.57273 > 127.0.0.1.8080: Flags [F.], cksum 0xfe28 (incorrect -> 0xe50f), seq 84347052, ack 2958367134, win 12759, options [nop,nop,TS val 1526558843 ecr 1525310591], length 0
00:56:40.220579 IP (tos 0x0, ttl 64, id 224, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3be2)!)
127.0.0.1.8080 > 127.0.0.1.57273: Flags [.], cksum 0xfe28 (incorrect -> 0xd900), seq 2958367134, ack 84347053, win 12759, options [nop,nop,TS val 1526558843 ecr 1526558843], length 0
00:56:40.220608 IP (tos 0x10, ttl 64, id 40943, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->9cc2)!)
127.0.0.1.57273 > 127.0.0.1.8080: Flags [.], cksum 0xfe28 (incorrect -> 0xd900), seq 84347053, ack 2958367134, win 12759, options [nop,nop,TS val 1526558843 ecr 1526558843], length 0
00:56:40.227383 IP (tos 0x0, ttl 64, id 49366, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->7beb)!)
127.0.0.1.8080 > 127.0.0.1.57273: Flags [F.], cksum 0xfe28 (incorrect -> 0xd8f9), seq 2958367134, ack 84347053, win 12759, options [nop,nop,TS val 1526558849 ecr 1526558843], length 0
00:56:40.227473 IP (tos 0x10, ttl 64, id 26769, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->d420)!)
127.0.0.1.57273 > 127.0.0.1.8080: Flags [.], cksum 0xfe28 (incorrect -> 0xd8f3), seq 84347053, ack 2958367135, win 12759, options [nop,nop,TS val 1526558849 ecr 1526558849], length 0
当进行关闭后,在进入Time_wait的状态的时候,此时要等待2MSL(2倍的最大报文生命周期),防止ack丢失,进行重传ack(另外一段超时重传fin包)