Linux網絡編程-UDP和TCP協議詳解

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1|0二. 引言","attrs":{}}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網絡協議是每個程序員都要掌握的基礎知識,幹啥都離不開網絡,就算在家裏新買了個路由器不是嗎,同事連不上網,你的女朋友手機沒有網看劇了正看到高潮部分,到那時候你打開百度......那嫌棄的你的眼神彷彿在說,就這?程序員連個網都不會修?以上都是臆想,以實際爲準.雖然看完本文章,你還是需要去百度怎麼修復網絡問題,但是你已經知道爲什麼會出現這種問題了!","attrs":{}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2|0二. UDP","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"UDP協議全稱是用戶數據報協議,在網絡中它與TCP協議一樣用於處理數據包,是一種無連接的協議.在OSI中,第四層傳輸層,處於IP協議的上一層UDP有不提供數據包分組,組裝和不能對數據包進行排序的缺點,也就是說,當報文發送後,無法監控其是否完整安全到達的,就想一個壞掉了的水龍頭,你不論怎麼讓他停止他都只會輸出,也不管你的桶滿沒滿,就像愛一樣","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"文章相關視頻講解:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"C/C++ Linux服務器開發高級架構師學習視頻點擊:","attrs":{}},{"type":"link","attrs":{"href":"https://ke.qq.com/course/417774?flowToken=1013189","title":null,"type":null},"content":[{"type":"text","text":"C/C++Linux服務器開發高級架構/Linux後臺架構師-學習視頻","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.bilibili.com/video/BV1Sz4y197Ro/","title":null,"type":null},"content":[{"type":"text","text":"底層原理到徒手實現 TCP/IP網絡協議棧","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.bilibili.com/video/BV17Z4y1s7xt/","title":null,"type":null},"content":[{"type":"text","text":"tcpip協議棧與網絡API的關聯,udp的併發性比tcp強?","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2|1特點","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1. 面向無連接:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先UDP是不需要和TCP一樣在發送數據前進行三次握手建立連接的,想發數據就可以開始發送了.並且也只是主句報文的搬運工,不會對數據報文進行任何拆分和拼接操作","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"具體來說:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在發送端,應用層將數據傳遞給UDP協議,UDP只會給數據增加一個UDP頭標識下UDP,然後就傳遞給網絡層了","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在接收端,網絡層將數據傳遞給傳輸層,UDP只去除IP報頭就傳遞給應用層,不會任何拼接操作","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2. 有單播,多播,廣播的功能","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"UDP不止支持一對一的傳輸方式,同樣支持一對多,多對多,多對一的方式,也就是說UDP提供了單播,多播和廣播的功能.","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3. UDP是面向報文的","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"發送方的UDP讀應用程序交下來的報文,在添加首部後就向下交付IP層.UDP對應用層交下來的報文,既不合並,也不拆分,而是保留這些報文的邊界.因此,應用程序必須選擇合適大小的報文","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4. 不可靠性","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先不可靠性體現在無連接上,通信不需要建立連接,想發就發,這樣的情況肯定不可靠","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"並且受到什麼數據就傳遞什麼數據,並且也不會備份數據,發送數據也不會關心對方是否已經正確接收到數據了","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"再者網絡環境時好時壞,但是UDP因爲沒有擁塞控制,一直會以恆定的速度發送數據,即使網絡條件不好,也不會對發送數據進行調整.這樣實現的弊端就是在網絡條件不好的情況下會導致丟包,但是有點也很明顯,比如電話會議等等最好就是UDP","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"5. 頭部開銷小,傳輸數據報文是很高效的.","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e3/e3ddc343a24fbc5b0960498ceb7dd982.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此UDP的頭部開銷小,只有八字節,相比TCP的至少二十字節要少得多,在傳輸數據報文時是很高效的","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3|0三. TCP/IP網絡模型","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"計算機網絡設備相互通信,雙方必須基於相同的方法.比如如何探測到通信目標,由那一邊先發起通信,使用哪種語言進行通信,怎樣結束通信等規則都需要實現確定.不同硬件,操作系統之間的通信,所有的這一切都需要一種規則,而我們吧這種規則成爲協議.","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TCP/IP","attrs":{}},{"type":"text","text":"是互聯網相關的各類協議的總稱,比如:**TCP,UDP,IP,FTP,HTTP,ICMP,SMTP **等都屬於 TCP/IP 族內的協議","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大家一定知道OSI七層模型,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TCP/IP","attrs":{}},{"type":"text","text":"概念模型是這樣的","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b1/b1a5e072a91a0014d7d355c03c93661d.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"應用層:負責向用戶提供應用程序.比如HTTP,FTP,Telnet,DNS,SMTP等","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"傳輸層:負責對報文進行分組和重組,並以TCP或UDP協議格式封裝報文","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"網絡層:負責路由以及把分組報文發送給目標網絡或主機","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"鏈路層:負責封裝和接縫IP報文,發送和接收ARP/RARP報文等","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4|0TCP","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當一臺計算機想要與另一臺計算機通訊時,兩臺計算機之間的通信需要暢通且可靠,這樣才能保證正確收發數據.例如當你收文件時候不希望收到的是一個損壞的文件,發過來的小電影是無法放映的,或者直接變成馬賽克,當然也可能本來就有馬賽克,這不是我們希望得到的,於是就用到了TCPTCP協議全稱是傳輸控制協議,,這是一種面向連接的,可靠的,基於字節流的傳輸層通信協議","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4|1TCP的連接過程","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/89/8917b162a01f39c50f568456b920c2f6.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP報文也分爲首部和數據兩部分,首部默認情況下一般是20字節長度,但在20字節長度,但在一些請求情況下,會使用\"可選字段\",這時,首部長度會有所增加,但最長不超過60字節","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"關於C/C++ Linux後端開發網絡底層原理知識 點擊 ","attrs":{}},{"type":"link","attrs":{"href":"https://jq.qq.com/?_wv=1027&k=6c8uFsnt","title":null,"type":null},"content":[{"type":"text","text":"學習資料","attrs":{}}]},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 獲取,內容知識點包括Linux,Nginx,ZeroMQ,MySQL,Redis,線程池,MongoDB,ZK,Linux內核,CDN,P2P,epoll,Docker,TCP/IP,協程,DPDK等等。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/56/5621814f9fe9d46b1e3167774f563eae.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"源端口 - 16bit","attrs":{}},{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來源處的端口號;端口號有65536個,即$2^{16}$。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"目的端口 - 16bit","attrs":{}},{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目的處的端口號","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"序號 - 32bit","attrs":{}},{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP在對數據進行分段的時候,會給每一個TCP報文段添加一個序號,序號字段的值其實是該文段所發送的數據的第一個字節的序號。這麼做的原因是,TCP是面向連接的可靠服務,這個序號可以保證數據在傳輸過程中保持有序性,接受端可以通過這個序號確認收到的數據的完整性和先後順序;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"確認號 - 32bit","attrs":{}},{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"確認號,是期望收到對方的下一個報文段的數據的第一個字節的序號;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"數據偏移 - 4bit","attrs":{}},{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實它本質上就是“首部長度”,因爲“數據偏移”是指TCP報文段的數據部分的起始處距離TCP報文段的起始處的距離。(仍然很拗口,但相信你能明白)。  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"數據偏移總共佔4bit,因此最大能表示的數值爲15。但TCP的報文頭部至少爲20字節。因此數據偏移的單位是“4字節”,此處的設計和IP數據報的設計是完全相同的,所以說TCP報文段首部的長度最長爲15×4=60字節,且首部長度必須爲4字節的整數倍。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"保留字段 - 6bit","attrs":{}},{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"IETF文檔指出,這6bit在標準中是保留字段,留待以後使用,必須爲0。我猜測,有兩個目的,第一個是預留除URG/ACK/PSH/RST/SYN/FIN/之外的冗餘功能位;第二個是爲了對齊字節位。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"控制位 - 6bit","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"又稱爲TCP flag,該字段從左到右分爲以下六個字段,指明包的類型。同時用於控制TCP的狀態機,同時ACK和SYN與三次握手協議有關,FIN與四次揮手協議有關。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"① 緊急字段URG - 1bit","attrs":{}},{"type":"text","text":"    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當URG=1時,此字段告訴系統此報文段中有緊急數據,應儘快傳送。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"② 確認字段ACK - 1bit","attrs":{}},{"type":"text","text":"    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當ACK=1時,表示確認,且確認號有效;當ACK=0時,確認號字段無效。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"③ 推送字段PSH - 1bit","attrs":{}},{"type":"text","text":"    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當PSH=1時,則報文段會被儘快地交付給目的方,不會對這樣的報文段使用緩存策略。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"④ 復位字段RST - 1bit","attrs":{}},{"type":"text","text":"    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當RST爲1時,表明TCP連接中出現了嚴重的差錯,必須釋放連接,然後再重新建立連接。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"⑤ 同步字段SYN - 1bit","attrs":{}},{"type":"text","text":"    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當SYN=1時,表示發起一個連接請求。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"⑥ 終止字段FIN - 1bit","attrs":{}},{"type":"text","text":"    ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用來釋放連接。當FIN=1時,表明此報文段的發送端的數據已發送完成,並要求釋放連接。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"窗口字段 - 16bit","attrs":{}},{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此字段用來控制對方發送的數據量,單位爲字節。  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般TCP連接的其中一端會根據自身的緩存空間大小來確定自己的接收窗口大小,然後告知另一端以確定另一端的發送窗口大小。該字段與TCP的流量控制服務有關。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"校驗和字段 - 16bit","attrs":{}},{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"與IP協議的檢驗和不同,TCP的這個校驗和是針對首部和數據兩部分的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"緊急指針字段 - 16bit","attrs":{}},{"type":"text","text":"  ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"緊急指針指出在本報文段中的緊急數據的最後一個字節的序號。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4|2三次握手四次揮手(這詞都聽吐了,換個叫法:一鍵三連,取消三連加取關/滑稽))","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"還有一點其實人家是Three-way handshake,三步握手四步揮手的,誰握手握三次,揮手揮四次的,/喫瓜","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TIP:注意箭頭的指向,結合文字進行理解,箭頭沒有問題","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/54/548b4bd5a283ae4581b2202e59afd99a.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1. 一鍵三連","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在三連之前服務器和客戶端都爲CLOSED狀態.通信開始前,雙方都得創建各自的傳輸控制塊(TCB)服務器創建完TCB後便進入LISTEN狀態此時準備接受客戶端發來的連接請求","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"點贊(第一次握手)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端向服務端發送連接請求報文段.該保溫段的頭部中SYN=1,ACK=0,seq=X.請求發送後客戶端便進入SYN-SENT狀態","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SYN = 1,ACK = 0表示該報文段位連接請求報文","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"x爲本次TCP通信的字節流的初始序號","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"TIP:","attrs":{}},{"type":"text","text":"TCP規定SYN = 1的報文段不能有數據部分,但要消耗一個序號","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"投幣(第二次握手)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"服務端收到連接請求報文段後,如果同意連接,則會發送一個應答:SYN = 1,ACK = 1,seq = y,ack = x + 1該應答發送完成後便進入SYN-RCVD狀態.","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SYN=1,ACK=1表示該報文段爲連接同意的應答報文","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"seq=y表示服務端作爲發送者時,發送字節流的初始序號","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ack=x+1表示服務端希望下一個數據報發送序號從x+1開始的字節","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"收藏(第三次握手)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當客戶端收到連接同意的應答後,還要向服務端發送一個確認報文段,表示服務端發來的連接同意應答已經收到該報文頭部爲:ACK = 1, seq = x + 1, ack = y + 1.客戶端發完這個報文後便進入ESTABLSHED狀態,服務端收到這個應答後也進入ESTABLISHED狀態,此時連接的建立完成","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2. 取消三連加取關","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP連接的釋放一共需要四部,因爲TCP連接時雙向的,因此在四次揮手中,前兩次揮手用於斷開一個方向的連接,後兩次揮手用於斷開另一方向連接","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"取消點贊(第一次揮手)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"若客戶端認爲數據發送完成,則它需要向服務端發送連接釋放請求.該請求只有報文頭,頭中攜帶的主要參數爲:FIN = 1,seq = u此時,客戶端將進入FIN-WAIT-1狀態","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"FIN = 1表示該保溫是一個連接釋放請求","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"seq = u,u - 1是A向B發送的最後一個字節的序號","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"取消投幣(第二次揮手)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"服務端收到連接釋放請求後,會通知相應的應用程序,告訴它客戶端向服務端這個方向的連接已經釋放.此時服務端進入CLOSE-WAIT狀態,並向客戶端發送連接釋放的回答,其報文頭包含:ACK = 1,seq = v, ack = u + 1","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ACK=1:除TCP連接請求報文段以外,TCP通信過程中所有數據報的ACK都爲1,表示應答。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"seq=v,v-1是B向A發送的最後一個字節的序號。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ack=u+1表示希望收到從第u+1個字節開始的報文段,並且已經成功接收了前u個字節","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端收到應答進入FIN-WAIT-2狀態,等待服務端發送連接釋放請求第二次揮手完成後,客戶端到服務端方向的連接已經釋放,服務端不會接受數據,客戶端也不會發送數據,但服務端到客戶端的連接仍然存在,服務端可以繼續向客戶端發送數據","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"取消收藏(第三次揮手)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當服務端發送完所有數據後,向客戶端發送連接釋放請求,請求頭:FIN = 1,ACK, seq = w,ack = u+1.服務端便進入LAST-ACK狀態.","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"取消關注(第四次揮手)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當客戶端收到釋放請求後向B發送確認應答,此時客戶端進入TiME-WAIT狀態,該狀態會持續2MSL(2分鐘)時間,若該時間沒有服務端重發請求的話,就進入CLOSED狀態,撤銷TCB.當服務端收到確認應答後,也進入CLOSED狀態撤銷TCB","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章