tcp連線建立於終止

2021-08-02 08:26:57 字數 3350 閱讀 5222

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同步序號

ffin

傳送方完成資料傳送

rrst

復位連線

ppsh

盡快能地將資料送往接收程序

.以上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包)

TCP連線的建立於釋放

tcp的連線是由三次握手來完成的。客戶端向伺服器發起連線 第一次握手,客戶端向伺服器傳送tcp包,syn標誌位置1,初始序號seq x。第二次握手,伺服器向客戶端傳送確認包ack應答,ack值為x 1 乙個syn占用乙個序號 同時這個應答包syn為置1,向客戶機傳送連線請求,初始序號seq y。第三...

TCP連線建立與終止

目錄 tcptcp連線建立 三次握手 tcp連線終止 四次揮手 tcp的狀態變遷圖 tcp是乙個全雙工的 面向連線的 可靠地傳輸協議,所以無論哪一方向另一方傳送資料之前,都需要先在兩者之間建立乙個連線 基於全雙工通訊所以存在tcp的半關閉狀態,也就是說斷開連線必須是雙方都關閉,否則就說這種狀態為半關...

TCP連線建立過程和TCP連線終止過程

1 當客戶端想要與伺服器端進行tcp通訊時,會首先傳送乙個syn同步包 tcp包頭的syn位被置位 並指明要連線的伺服器端口號。2 如果伺服器端同意連線,就會向客戶端傳送乙個ack確認包 tcp包頭的ack被置位 表示同意客戶端的請求,同時將該包的syn位置位,表示伺服器也想與客戶端通訊。3 客戶端...