TCP报文格式
簡介
TCP 是傳輸層協(xié)議,為應(yīng)用程序提供了穩(wěn)定可靠的數(shù)據(jù)傳輸。它是一個滑動窗口協(xié)議,提供了超時和重傳的處理。
TCP 是在兩個端點之間建立的全雙工虛擬連接。每個端點由 IP 地址和端口號定義。
數(shù)據(jù)以字節(jié)流的形式傳輸,字節(jié)流按段傳輸。窗口大小決定了在需要接收方確認之前可以發(fā)送的數(shù)據(jù)字節(jié)數(shù)。
TCP/IP 協(xié)議的模型
首先,我們 TCP/IP 協(xié)議棧是一個三層模型的結(jié)構(gòu),包括傳輸層、網(wǎng)絡(luò)層、數(shù)據(jù)鏈路層。在 TCP/IP 之上的是應(yīng)用層,比較常用的就是 HTTP 協(xié)議。
在圖中,沒有被客戶端和服務(wù)端圈中的方格,代表的是數(shù)據(jù)報。
其中,粉色的部分是當(dāng)前協(xié)議的請求頭,綠色的部分對當(dāng)前協(xié)議而言就是數(shù)據(jù)部分。
舉例說明:
以 HTTP 協(xié)議為例,客戶端發(fā)送數(shù)據(jù)時,先封裝一層 HTTP Header + HTTP Data,然后交給傳輸層。
傳輸層協(xié)議把應(yīng)用層傳遞過來的數(shù)據(jù)整體當(dāng)做 Data,再加上了 TCP Header,封裝成數(shù)據(jù)報,然后交給網(wǎng)絡(luò)層。
網(wǎng)絡(luò)層再在傳輸層數(shù)據(jù)的基礎(chǔ)上,加上 IP Header,封裝后交給數(shù)據(jù)鏈路層。
數(shù)據(jù)鏈路層,將數(shù)據(jù)加上 MAC Header 之后,從客戶端傳送給服務(wù)端。
服務(wù)端接收到數(shù)據(jù)后,一層層剝離 Header 并且根據(jù) Header 中的協(xié)議內(nèi)容處理后,再將數(shù)據(jù)傳遞給上層進行操作。
數(shù)據(jù)傳遞到應(yīng)用層后,由 HTTP 協(xié)議解析完成后,處理 Request 請求,并且生成相應(yīng)的 Response,然后再反過來傳遞給客戶端。
最終客戶端(瀏覽器)中就會展示相應(yīng)的網(wǎng)頁。
TCP 報文格式
二進制位報文格式圖:
Source Port. 16 bits. 發(fā)送端端口號
Destination Port. 16 bits. 接收端(目的地端)端口號
Sequence Number. 32 bits.
The sequence number of the first data byte in this segment. If the SYN bit is set, the sequence number is the initial sequence number and the first data byte is initial sequence number + 1.
序列號,當(dāng)前段的第一個字節(jié)的序列號。如果設(shè)置了 SYN 同步標(biāo)記(即 SYN=1),序列號的值是初始序列號。帶 SYN 同步標(biāo)記的段的第一個字節(jié)的序列號=初始序列號+1。
初始序列號(ISN)是隨機的,網(wǎng)上很多圖片上的Seq=0,其實指的是相對序列號。
為什么初始序列號是隨機的,而不是固定的? 參考 初始化序列號(ISN)
Acknowledgment Number. 32 bits. 確認編號,回執(zhí)號
If the ACK bit is set, this field contains the value of the next sequence number the sender of the segment is expecting to receive. Once a connection is established this is always sent.
如果設(shè)置了 ACK 確認標(biāo)記(ACK=1),確認編號的值等于接收端希望收到的下一個字節(jié)流段的序列號,也等于接收端收到的由發(fā)送端發(fā)來的上一個字節(jié)流段的序列號 + 1。一旦客戶端和服務(wù)端的連接建立起來,就會一直發(fā)送確認編號。
Data Offset. 4 bits.
The number of 32-bit words in the TCP header. This indicates where the data begins. The length of the TCP header is always a multiple of 32 bits.
TCP Header 中, 32-bit words 的數(shù)量,也就是本節(jié)中的 TCP 二進制位報文格式圖的行數(shù)。這個字段表明了數(shù)據(jù)從什么地方開始。TCP 報頭的長度始終是 32 位的倍數(shù)。
reserved. 3 bits. 保留字段
Must be cleared to zero.
ECN, Explicit Congestion Notification. 3 bits. 顯式擁塞通知
Added in RFC 3168.
N, NS, Nonce Sum. 1 bit.
Added in RFC 3540. This is an optional field added to ECN intended to protect against accidental or malicious concealment of marked packets from the TCP sender.
這是添加到ECN的可選字段,用于防止來自TCP發(fā)送者的標(biāo)記數(shù)據(jù)包的意外或惡意隱藏。
C, CWR. 1 bit.
A Congestion Window Reduced (CWR) flag in the TCP header so that the data sender can inform the data receiver that the congestion window has been reduced.
TCP報頭中的減少擁塞窗口(CWR)標(biāo)志,以便數(shù)據(jù)發(fā)送者可以通知數(shù)據(jù)接收者擁塞窗口已經(jīng)減少。
E, ECE, ECN-Echo. 1 bit.
An ECN-Echo (ECE) flag in the TCP header so that the data receiver can inform the data sender when a CE packet has been received.
TCP報頭中的ECN-Echo(ECE)標(biāo)志,以便數(shù)據(jù)接收器可以在接收到CE數(shù)據(jù)包時通知數(shù)據(jù)發(fā)送者。
CE: Routers can instead set the Congestion Experienced (CE) codepoint in the IP header of packets from ECN-capable transports.
路由器可以在支持ECN的傳輸包的IP報頭中設(shè)置擁塞經(jīng)歷(CE)碼位。
Control Bits. 6 bits. 控制位
U, URG. 1 bit.
Urgent pointer valid flag.
A, ACK. 1 bit.
Acknowledgment number valid flag.
P, PSH. 1 bit.
Push flag.
R, RST. 1 bit.
Reset connection flag.
重置連接標(biāo)識
S, SYN. 1 bit.
Synchronize sequence numbers flag.
同步序列號標(biāo)識
F, FIN. 1 bit.
End of data flag.
結(jié)束數(shù)據(jù)傳輸標(biāo)識
Window. 16 bits, unsigned.
The number of data bytes beginning with the one indicated in the acknowledgment field which the sender of this segment is willing to accept.
此段的發(fā)送方愿意接受的以確認字段中指示的字節(jié)開始的數(shù)據(jù)字節(jié)數(shù)。
Checksum. 16 bits.
This is computed as the 16-bit one's complement of the one's complement sum of a pseudo header of information from the IP header, the TCP header, and the data, padded as needed with zero bytes at the end to make a multiple of two bytes. The pseudo header contains the following fields:
這是作為IP頭,TCP頭和數(shù)據(jù)中的偽信息頭的補碼之和的16位補碼計算得出的,根據(jù)需要用最后的零字節(jié)填充以形成兩個字節(jié)的倍數(shù) 。 偽頭包含以下字段:
Urgent Pointer. 16 bits, unsigned.
If the URG bit is set, this field points to the sequence number of the last byte in a sequence of urgent data.
如果URG位置1,則此字段指向緊急數(shù)據(jù)序列中最后一個字節(jié)的序列號。
Options. 0 to 40 bytes.
Options occupy space at the end of the TCP header. All options are included in the checksum. An option may begin on any byte boundary. The TCP header must be padded with zeros to make the header length a multiple of 32 bits.
選項占據(jù)TCP報頭末尾的空間。 所有選項都包含在校驗和(checksum)中。選項可以在任何字節(jié)邊界上開始。TCP報頭必須用零填充,以使報頭長度為32位的倍數(shù)。
Data. Variable length. 數(shù)據(jù)部分
十六進制圖
每 1 個十六進制數(shù)位可以表示 4 位 bits 位。轉(zhuǎn)換結(jié)果如下
TCP 狀態(tài)集
參考文檔
www.networksorcery.com/enp/protocol/tcp.htm
擴展閱讀
最詳細的http協(xié)議、tcp/ip協(xié)議
TCP報文解析
總結(jié)
- 上一篇: CDS view里case - when
- 下一篇: 三维建模软件3dmax快捷键怎么根据需要