從一個HTTP請求來讀懂HTTP、TCP協議

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"從一個HTTP請求來看網絡分層原理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"兩臺主機間會通過非常多網絡設備,不管哪個網絡設備都會發生數據丟失,如果發生數據丟失的話,會發生數據重傳,會出現數據重複(之前丟失的包並不是丟失而是產生了延時)。數據傳輸的介質也可能多樣,如內網裏通過網線進行傳輸,連接到公網的話會通過光纖進行連接,所以要實現不同介質間信號的轉換,還有從光纖到路由器無線脈衝轉換,距離遠的話還有信號衰減問題。所以在網絡傳輸過程中有非常多的問題需要解決,把問題分組分層,不同層次間解決不同問題,不同層次間定義標準化接口讓它們間可以進行數據的通信。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"複雜的網絡"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s2.51cto.com/oss/202010/20/2ca881c90ac1021517ea89c524ddd692.png-wh_600x-s_379608925.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/89/898d19e0668f3b2d5ce640b5c293f186.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s2.51cto.com/oss/202010/20/2ca881c90ac1021517ea89c524ddd692.png-wh_600x-s_379608925.png","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了簡化網絡的複雜度,網絡通信的不同方面被分解爲多層次結構,每一層只與緊挨着的上層或者下層進行交互,將網絡分層,這樣就可以修改,甚至替換某一層的軟件,只要層與層之間的接口保持不變,就不會影響到其他層。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"OSI( Open System Interconnection Reference Model): 開放系統互聯參考模型"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP/IP 協議族"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"OSI七層理論體系結構"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s6.51cto.com/oss/202010/20/cce1692da335fa5d2dde2a8a30f93227.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e7/e723e4d74254b14ae715a21e7f3abdd6.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s6.51cto.com/oss/202010/20/cce1692da335fa5d2dde2a8a30f93227.png","title":null}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"物理層:"},{"type":"text","text":"解決兩臺主機的通信問題—A往B發送比特流(0101),B能接收到這些比特流。定義了物理設備的標準如網線的類型,光纖的接口類型以及傳輸介質的傳輸速率等。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"數據鏈路層:由於物理層上的傳輸的比特流可能會出現錯傳、誤傳等,所以數據鏈路層定義瞭如何格式化數據即將比特流封裝成幀,提供了錯誤檢測。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"網絡層:"},{"type":"text","text":"隨着節點的增加,點對點通信是需要經過多個節點的,如何找到目標節點,如何找到最優路徑變成爲了首要需求。所以出現了網絡層,主要目的是將網絡地址翻譯成對應的物理地址,分組傳輸、路由選擇,本層的傳輸單位是數據報(分組),本層需要注意的TCP/IP協議中的TCP協議。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"傳輸層:"},{"type":"text","text":"隨着網絡需要的進一步擴大,通信過程中需要傳輸大量的數據,網絡可能會發生中斷,爲了保證傳輸大量文件時的準確性,需要對發送的數據進行切分,切分成一個個的segment進行發送,考慮如何在接受方拼接切分的segment組成完整的數據,以及發現丟失segment時該如何處理,需要注意的協議TCP、UDP。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"會話層:"},{"type":"text","text":"不同機器上的用戶之間建立以及管理會話。用於保證應用程序自動收發包和尋址。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":6,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"表示層:"},{"type":"text","text":"信息的語義語法,加密解密,轉換翻譯,壓縮解壓縮。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":7,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"應用層:"},{"type":"text","text":"規定雙方必須使用固定長度的消息頭,且消息頭必須記錄消息長度等信息。需要注意的是TCP/IP協議中的HTTP協議。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s2.51cto.com/oss/202010/20/7f70bcd8b566495c50709b65918b2708.png-wh_600x-s_3042196636.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/73/733b6578e18e8e4c3f4f1c3ee49d60c3.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s2.51cto.com/oss/202010/20/7f70bcd8b566495c50709b65918b2708.png-wh_600x-s_3042196636.png","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"TCP/IP四層模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"是OSI的一種實現,包括應用層、運輸層、網際層和網絡接口層。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s2.51cto.com/oss/202010/20/098fa5e287316c1fd52125b9112cb8e4.png-wh_600x-s_3103511319.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/76/76cb34519bd4344cb7fc86c2d0e7bc91.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s2.51cto.com/oss/202010/20/098fa5e287316c1fd52125b9112cb8e4.png-wh_600x-s_3103511319.png","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"一個HTTP請求的分層解析流程"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s2.51cto.com/oss/202010/20/eac5b8bcad9162d4ec75ccecf4c29ea8.png-wh_600x-s_3348559328.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f7/f752f69ead1f8d1e76e9e14bd55193c7.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s2.51cto.com/oss/202010/20/eac5b8bcad9162d4ec75ccecf4c29ea8.png-wh_600x-s_3348559328.png","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上圖右邊一個服務器部署了一個靜態頁面,通過nginx部署在公網上,瀏覽器通過域名對它進行訪問,瀏覽器輸入域名點回車後是怎麼工作的呢?"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"http://www.dumain.com "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"服務端只認ip地址,瀏覽器將域名解析出來,看下瀏覽器裏有沒有域名對應DNS的緩存,有的話直接拿到服務端的ip地址,沒有的話去本地的host文件看有沒有配置,沒有配置的話纔會發起一個DNS請求用來獲取服務器ip地址。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DNS也是臺服務器也有自己的ip地址,這時候應用層會構造一個DNS請求報文,應用層會去調用傳輸層的接口一個socket的API,DNS默認使用UDP實現數據傳輸,即應用層調用傳輸層的API,傳輸層會在DNS請求報文基礎上加一個UDP的請求頭,傳輸層將數據交給網絡層,網絡層同樣在UDP請求報文基礎上加IP的請求頭,網絡層會將IP請求報文交給數據鏈路層,數據鏈路層會將自己的mac頭加上去並把對應的請求報文交給下一個機器的mac地址也會加上去,下一個機器的mac地址通過網絡層ARP協議找到,ARP會發送一些請求看下你對應的ip地址的mac地址是多少,最後通過物理層物理介質傳出去,通常傳到路由器上."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"路由器是三層設備(從下向上)從物理層開始連接,物理層交給數據鏈路層,數據鏈路層看下地址是不是給我的,是給我的進行解析,不是給我的就丟棄,報文再傳給上面一層網絡層,網絡層把數據傳到下一個路由器的地址是多少,會通過運營商的網絡接口傳到運營商的路由器上,運營商有自己的DNS服務器,如果配置的是運營商自己的DNS服務器的話會直接在這個DNS服務器裏找自己對應的域名拿到對應的ip地址,也就是剛請求DNS報文地址,然後原路返回解析直到應用層拿到剛域名對應的ip地址,這樣就可以進行HTTP請求報文的發送,再調用傳輸層協議是TCP參數,同樣每到一層加頭。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"HTTP"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"什麼是HTTP?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"超文本傳輸協議,是一個基於請求與響應,無狀態的,應用層的協議,常基於TCP/IP協議傳輸數據,互聯網上應用最爲廣泛的一種網絡協議,所有的WWW文件都必須遵守這個標準。設計HTTP的初衷是爲了提供一種發佈和接收HTML頁面的方法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"HTTP特點"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"無狀態:協議對客戶端沒有狀態存儲,對事物處理沒有“記憶”能力,比如訪問一個網站需要反覆進行登錄操作。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"無連接:HTTP/1.1之前,由於無狀態特點,每次請求需要通過TCP三次握手四次揮手,和服務器重新建立連接。比如某個客戶機在短時間多次請求同一個資源,服務器並不能區別是否已經響應過用戶的請求,所以每次需要重新響應請求,需要耗費不必要的時間和流量。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"基於請求和響應:基本的特性,由客戶端發起請求,服務端響應。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"簡單快速、靈活。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"通信使用明文、請求和響應不會對通信方進行確認、無法保護數據的完整性。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HTTP協議版本已經演化到3.0版本,關於協議版本可以查看 快速掌握HTTP1.0 1.1 2.0 3.0的特點及其區別"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"HTTP報文格式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HTTP 協議的請求報文和響應報文的結構基本相同,由三大部分組成:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"起始行(start line):描述請求或響應的基本信息"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"頭部字段集合(header):使用 key-value 形式更詳細地說明報文"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"消息正文(entity):實際傳輸的數據,它不一定是純文本,可以是圖片、視頻等二進制數據"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其中起始行和頭部的字段併成爲 請求頭 或者 響應頭,統稱爲 Header;消息正文也叫實體,稱爲 body。HTTP 協議規定每次發送的報文必須要有 Header,但是可以沒有 body,也就是說頭信息是必須的,實體信息可以沒有。而且在 header 和 body 之間必須要有一個空行(CRLF)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s4.51cto.com/oss/202010/20/1a566786f4f87b134c732c45cca957cb.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/72/729ca82aac598f042a848f9ba59293f9.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s4.51cto.com/oss/202010/20/1a566786f4f87b134c732c45cca957cb.png","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"請求行報文格式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s4.51cto.com/oss/202010/20/289db58a4e8ba25b6460fb0f44648815.png-wh_600x-s_2637844719.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f7/f71d705041c02d0b1887cd8d5fc19760.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s4.51cto.com/oss/202010/20/289db58a4e8ba25b6460fb0f44648815.png-wh_600x-s_2637844719.png","title":null}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"請求方法:如 GET/HEAD/PUT/POST,表示對資源的操作;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"請求目標:通常是一個 URI,標記了請求方法要操作的資源;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"版本號:表示報文使用的 HTTP 協議版本。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"響應報文格式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s6.51cto.com/oss/202010/20/2a016c6c4e954d755a429cc3edfdaba6.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/94/94c457bf22bcba348313c53c41493ede.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s6.51cto.com/oss/202010/20/2a016c6c4e954d755a429cc3edfdaba6.png","title":null}}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"版本號:表示報文使用的 HTTP 協議版本;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"狀態碼:一個三位數,用代碼的形式表示處理的結果,比如 200 是成功,500 是服務器錯誤;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原因:作爲數字狀態碼補充,是更詳細的解釋文字,幫助人理解原因。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"請求及響應報文格式對比"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s3.51cto.com/oss/202010/20/239df0ef71358728caeb8272490eb0bb.png-wh_600x-s_4035336112.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7d/7debbcca603827b57b4d432b5abf4d74.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s3.51cto.com/oss/202010/20/239df0ef71358728caeb8272490eb0bb.png-wh_600x-s_4035336112.png","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"HTTP 頭字段"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"頭部字段是 key-value 的形式,key 和 value 之間用“:”分隔,最後用 CRLF 換行表示字"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"段結束。比如前後分離時經常遇到的要與後端協商傳輸數據的類型“Content-type: application/json”,這裏 key 就是“Content-type”,value 就 是“application/json”。HTTP 頭字段非常靈活,不僅可以使用標準裏的 Host、 Connection 等已有頭,也可以任意添加自定義頭,這就給 HTTP 協議帶來了無限的擴展可能。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"頭字段注意事項"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"字段名不區分大小寫,字段名裏不允許出現空格,可以使用連字符“-”,但不"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"能使用下劃線“_”(有的服務器不會解析帶“_”的頭字段)。字段名後面必須緊接 着“:”,不能有空格,而“:”後的字段值前可以有多個空格;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"字段的順序是沒有意義的,可以任意排列不影響語義;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"字段原則上不能重複,除非這個字段本身的語義允許,例如 Set-Cookie。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HTTP 協議中有非常多的頭字段,但基本上可以分爲四大類:通用標頭、實體標頭、請求標頭、響應標頭。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"HTTP 頭字段更多內容請查看《深入掌握HTTP四種標頭基本概念 》"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"TCP 協議"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP(Transmission Control Protocol),傳輸控制協議:面向連接的,可靠的,基於字節流的傳輸層通信協議。它能幫助你確定計算機連接到 Internet 以及它們之間的數據傳輸。通過三次握手來建立 TCP 連接,三次握手就是用來啓動和確認 TCP 連接的過程。一旦連接建立後,就可以發送數據了,當數據傳輸完成後,會通過關閉虛擬電路來斷開連接。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"TCP特點"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基於連接的:數據傳輸之前需要建立連接"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"全雙工的:雙向傳輸"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"字節流:不限制數據大小,打包成報文段,保證有序接收,重複報文自動丟棄"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"流量緩衝:解決雙方處理能力的不匹配"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可靠的傳輸服務:保證可達,丟包時通過重發機制實現可靠性"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"擁塞控制:防止網絡出現惡性擁塞"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"TCP報文格式"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/78/7862a3d83660617994b727d4ca41ec3b.png","alt":"從一個HTTP請求來讀懂HTTP、TCP協議","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"16位源端口/16位目的端口:"},{"type":"text","text":"負責實現應用程序之間的數據傳輸"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"32位序號/32位確認序號:"},{"type":"text","text":"用於實現tcp在傳輸層的包序管理——tcp有序交付數據"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"4位頭部長度:"},{"type":"text","text":"以4個字節爲單位;4位保存的最大數字是15;因此tcp報頭最大長度是15*4=60個字節"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"6位保留位;"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"6位標誌:"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"URG——緊急指針標誌"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ACK——確認回覆標誌"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"PSH——提示立即接受位"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RST——重置連接位"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"SYN——連接建立請求位"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"FIN——斷開連接請求位"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"16位窗口大小:"},{"type":"text","text":"滑動窗口機制–>流量控制–>告訴對端所能發送的最大數據量"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"校驗和:"},{"type":"text","text":"二進制反碼求和–>校驗數據一致性"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"緊急指針:"},{"type":"text","text":"指明哪些數據是緊急數據"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"選項數據:"},{"type":"text","text":"三次握手時,協商MSS大小的數據"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP連接:四元組[ 源地址, 源端口, 目的地址, 目的端口 ]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"TCP三次握手"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同步通信雙方初始序列號( ISN, initial sequence number )"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"協商TCP通信參數(MSS, 窗口信息,指定校驗和算法)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在瞭解具體流程之前,我們先認識幾個概念 "}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/54/54464d86da72c012c1f981d1978cf73c.png","alt":"從一個HTTP請求來讀懂HTTP、TCP協議","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最初兩端的TCP進程都處於CLOSED關閉狀態,A主動打開連接,而B被動打開連接。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"A、B關閉狀態CLOSED — B收聽狀態LISTEN — A同步已發送狀態SYN-SENT — B同步收到狀態SYN-RCVD— A、B連接已建立狀態ESTABLISHED"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"B的TCP服務器進程先創建傳輸控制塊TCB,準備接受客戶進程的連接請求。然後服務器進程就處於LISTEN(收聽)狀態,等待客戶的連接請求。若有,則作出響應。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"SYN:"},{"type":"text","text":"它的全稱是 Synchronize Sequence Numbers ,同步序列編號。是 TCP/IP 建立連接時使用的握手信號。在客戶機和服務器之間建立 TCP 連接時,首先會發送的一個信號。客戶端在發送 SYN 消息時,就會在自己的段內生成一個隨機值 X。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"SYN-ACK:"},{"type":"text","text":"服務器收到 SYN 後,應答客戶端連接,發送一個 SYN-ACK作爲答覆。確認號設置爲比接收到的序列號多一個,即 X + 1,服務器爲數據包選擇的序列號是另一個隨機數 Y。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"ACK:"},{"type":"text","text":" Ackowledge character ,確認字符,表示發來的數據已確認接收無誤。最後客戶端將 ACK 發送給服務器。序列號被設置爲所接收的確認值即 Y+ 1。 "}]}]}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/58/58c0a55095934162bc7e603721060170.png","alt":"從一個HTTP請求來讀懂HTTP、TCP協議","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面通過一個案例看三次握手是怎麼進行的"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Nginx服務器部署一個靜態頁面(我的端口爲:8000)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s5.51cto.com/oss/202010/20/b33c078e3438bedf379fc11555c71777.png-wh_600x-s_652675029.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/81/81f6f6d73abc8a8b8dde071ff303c581.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s5.51cto.com/oss/202010/20/b33c078e3438bedf379fc11555c71777.png-wh_600x-s_652675029.png","title":null}},{"type":"text","text":"  "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"tcpdump指定網卡進行監聽抓取報文"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"tcpdump -i en0 -S -c 3 port 8000 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在客戶端使用nc網絡工具發送一個請求"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"nc 192.168.109.200 8000 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"三次握手監聽結果如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s6.51cto.com/oss/202010/20/250321025bce0f30c37d401a6f168fb0.png-wh_600x-s_1745764668.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ca/ca15db083f27b26375c2da7cf9ce02b3.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s6.51cto.com/oss/202010/20/250321025bce0f30c37d401a6f168fb0.png-wh_600x-s_1745764668.png","title":null}},{"type":"text","text":" "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"內核在三次握手做的一些事情,如下:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8a/8a5bbf5077c3a41065e79354ffd5c422.png","alt":"從一個HTTP請求來讀懂HTTP、TCP協議","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"連接狀態查看"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"netstat -tpn # t:TCP連接裝,p:進程顯示 ,n:數字形式  # 每秒查看一次 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"netstat -tpn -c 1"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s2.51cto.com/oss/202010/20/4eebbb5d4c1df852206917abd65a3155.png-wh_600x-s_3237275751.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ca/ca15db083f27b26375c2da7cf9ce02b3.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s2.51cto.com/oss/202010/20/4eebbb5d4c1df852206917abd65a3155.png-wh_600x-s_3237275751.png","title":null}},{"type":"text","text":" "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP四次揮手"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s4.51cto.com/oss/202010/20/82431c09da98b1e18fcd78d3c741ee1d.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f4/f4199b9e21d0430f2bff734a5080db2a.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s4.51cto.com/oss/202010/20/82431c09da98b1e18fcd78d3c741ee1d.png","title":null}},{"type":"text","text":" "}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"A: 發送FIN數據包,代表A不再發送數據"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"B: 收到請求,開始應答 ,避免了A重新發送FIN重試(應答機制)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"B: 處理完數據之後關閉,關閉連接,及發送FIN請求"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"A: 收到請求後發送ACK應答,B服務可以釋放連接"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"等待 2MSL後釋放連接"}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"防止報文丟失,導致B重複發送FIN"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"防止滯留在網絡中的報文,對新建立的連接造成數據擾亂"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"字節流的協議"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"TCP把應用交付的數據僅僅看成是一連串的無結構的字節流,TCP並不 知道字節流的含義,TCP並不關心應用程序一次將多大的報文發送到 TCP的緩存中,而是根據對方給出的窗口值和當前網絡擁堵的程度來決 定一個報文段應該包含多少個字節。"}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"MSS: Max Segment Size, 默認 536byte 實際數據 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "},{"type":"link","attrs":{"href":"https://s6.51cto.com/oss/202010/20/6a32cd810c727c3d7a48ad34605a4927.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/77/77f3549184238d5b62c30b0e3b497acf.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s6.51cto.com/oss/202010/20/6a32cd810c727c3d7a48ad34605a4927.png","title":null}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在網絡傳輸過程中可能會出現以下的一些情況:"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端一段時間沒有收到 ack 消息則重傳"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果緩衝區滿了則可能丟包或延時都需要重傳"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根據報文 sequeence number 字段重排序,還需要丟棄重複包。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"數據傳輸的可靠性"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"停止等待協議如下: "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s3.51cto.com/oss/202010/20/3d92bc12b5c0a1681c07a7e83e38c0bf.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/77/77f3549184238d5b62c30b0e3b497acf.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s3.51cto.com/oss/202010/20/3d92bc12b5c0a1681c07a7e83e38c0bf.png","title":null}},{"type":"text","text":" "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"停止等待協議,效率比較低"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"重傳機制如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ack 報文丟失"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d5/d5a3acdf585df89055dec51c6e2d8dd3.png","alt":"從一個HTTP請求來讀懂HTTP、TCP協議","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"請求報文丟失"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f8/f84c5a0f47952b1ae629f5a81879cf3d.png","alt":"從一個HTTP請求來讀懂HTTP、TCP協議","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"滑動窗口協議與累計確認(延時ack)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如上效率低,所以tcp提出了新的協議-滑動窗口協議與累計確認(延時ack)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"滑動窗口大小同通過tcp三次握手和對端協商,且受網絡狀況影響。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面是一個一個報文,實際可發一批報文,服務器並不是挨個去確認,上面回一個ack浪費資源,單獨響應一個報文時,tcp本身一個報文至少20個字節再加上ip頭報文20字節,所以一個ack至少40字節。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以延時ack的發送,如下圖確認最後一個報文如5就可以,但這樣也有一個問題如3的報文丟了,這時只能確認1和2連續報文,從3以後的報文全要重傳,已確認的報文在緩衝區丟棄掉。 "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://s6.51cto.com/oss/202010/20/cf6792bf21872507abdce42c1d5e9ffc.png","title":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e1/e1d94e25d65006fac26794493198094b.png","alt":"","title":"","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者:一角錢技術"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來源:https://www.toutiao.com/i6885366707725009419/"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章