Transmission Control Protocol (TCP)
概述
TCP為 傳輸層 的一種協定,因為是可靠傳輸,相較於 UDP 複雜許多,他有以下幾個機制:
- 連線導向(Connection-oriented)
- 全雙工服務(Full-duplex service)
- 可靠資料傳輸(Reliable data transfer)
- 估計來回時間
- 倍增逾時間隔
- 快速重送(Fast Retransmit)
- 流量控制(Flow Control)
- 壅塞控制(Congestion Control)
假設某台主機上的行程(用戶端行程)想要與另一台主機(伺服端行程)開啟連線,而建立TCP連線大概的步驟如下:
- 先透過三次握手(three-way handshake) 建立連線,TCP連線建立後就可以開始傳送資料
- 用戶端行程透過socket送出資料至傳送緩衝區(send buffer)
- 從緩衝區中抓出資料放入區段(segment) 中,要注意的是資料量有限制,受限於最大區段大小(Maximum Segment Size, MSS) ,而MSS的大小通常又是由最大連結層訊框,又稱為最大傳輸單元(Maximum transmission unit, MTU) 決定。 所以當TCP要傳送大型檔案時,通常會將檔案切割成大小為MSS的片段。
- 建立TCP區段(TCP segment) ,為資料加上標頭後送入網路層。
- 另一端收到後,資料會先放在接收緩衝區(Receive buffer) 。
- 應用程式再從緩衝區讀出資料。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
TCP標頭通常是20個位元組,結構如下所示:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 來源端與目的端埠號(SrcPort and DstPort number)
- 序號(SequenceNum) - 32 bits
紀錄第一個位元組的編號。假設現在要傳送30000位元組的檔案而MSS為1000位元組,所以檔案會被分成30個segment,而第一個segment的序號為0,第二個segment為1000,第三個為2000,以此類推。
- 確認編號(Acknowledgment) - 32 bits
//TODO
- 標頭長度(HdrLen) - 4 bits
紀錄標頭的長度,通常Options是空的,所以一般TCP標頭長度為20個bytes。
- FLAG - 6 bits
URG、ACK、PSH、RST、SYN、FIN 6個bits。
- 接收窗格(AdvertisedWindow)
- 檢查和(Checksum)
TCP的建立與關閉 Connection Establishment and Termination
建立連線
TCP透過三次握手(three-way handshake) 建立連線,有以下3個步驟
-
Step 1
用戶端傳送一個不含資料的TCP區段到伺服端,該區段的FLAG欄位中的SYN位元為1,且會隨機選定一個初始序號(client_isn)
-
Step 2
伺服端收到後配置TCP緩衝區與變數,並送出連許可區段給用戶端
- 確認客戶端的序號:ACK=1,且Acknowledgment欄位為client_isn+1。
- 傳送自己的初始序號:SYN=1,序號欄位為自己的初始序號server_isn。
-
Step 3
用戶端收到連線許可區段後,配置緩衝區與變數給該筆TCP連線,SYN=0、ACK=1,Acknowledgment欄位為server_isn+1。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
這三個步驟都完成後,雙方就可以開始互相傳送資料了。之後的資料,SYN位元都會被設定成0。
關閉連線
傳輸資料一段時間後,任一方都可結束該筆連線,假設現在用戶端想結束,步驟如下:
- 用戶端送出特殊TCP區段,FLAG內的FIN位元為1。
- 伺服端收到後,傳回確認區段給用戶端。
- 接著伺服端再送出FIN位元為1的TCP區段給用戶端。
- 用戶端收到後傳回確認區段結束這筆連線。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
來回時間的評估
在逾時就重送的機制哩,設定逾時間隔是很值得討論的議題,他要比來回時間(RTT)長,但又不能太長,所以估計RTT就變得很重要。TCP的RTT估計如下所示:
-
當收到ACK時,TCP就會記下發出packet跟收到ACK的時間間隔,記為。
-
任意時間的都有可能不同,所以TCP會持續更新一個估計值:
根據RFC6298,的建議值為0.125。
-
TCP也會估計RTT的變動程度,這個是SampleRTT跟EstimatedRTT的差距:
的建議值為0.25。
-
逾時間隔就被定義為:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →