建立 TCP 连接需要多少数据?

How much data it cost to set up a TCP connection?

我正在构建一个应用程序,我的 phone 经常向我的服务器发送数据。由于我将使用我的移动数据,我想知道建立(和拆除?)到我的服务器的 TCP 连接需要多少数据。

TCPThree-way握手

Device 1 sends its TCP sequence number and maximum segment size to Device 2.

Device 2 responds by sending its sequence number and maximum segment size to Device 1.

Device 1 acknowledges receipt of the sequence number and segment size information.

每个数据包由一个IPheader和数据(有效负载)组成。在这种情况下,数据部分包含 TCP。 TCP header 包含各种字段,包括源端口和目标端口、序列号和确认号、window 大小、TCP 标志、紧急指针和保留位。

与 IP header 一样,TCP header 也可能包含选项。 (注意 TCP 选项和 IP 选项是两个不同的东西。)因为 TCP 选项改变了 TCP header 的长度,长度设置在 header.

IPv4 header 是五个 4 字节块,或总共 20 个字节。

TCP 通常通常使用 header 的 24 字节 进行握手(前两个数据包),大约 20 用于正常数据包传输。

Maximum Segment Size (MSS): 4 bytes

Window Scale (WSCALE): 3 bytes

Timestamp (TS): 10 bytes

No Operation (NOP): 1 byte

Selective Acknowledgment Permitted (SackOK): 2 bytes

Selective Acknowledgment Data: 10 bytes (plus 8 bytes for each additional pair of sequence numbers)

终止连接

尽管使用 3 次握手建立连接只需要传输 3 个数据包,但断开连接需要 4 个!

  • 在第一帧中,客户端发送一个带有 ACK 的 FIN。设置了FIN参数,会通知服务器没有数据可以发送了。
  • 响应(第 2 帧)只是服务器确认客户端发送的 FIN。
  • 即使两台计算机之间建立了TCP连接,但连接仍然是相互独立的。所以服务器端也会传一个FIN给客户端。
  • 你猜对了...客户端会在最后一个数据包中确认服务器的 FIN。

每个帧的偏移量通常为 20 字节。

总结一下。

正在建立连接:~ 128-136 字节

断开连接:~ 160 字节

如果打算使用TLS/SSL握手,这个估计在4.5k-6.5k之间。

注:也请看一下TCP/IP Header Compression

来源:

Inside the TCP Handshake

Explanation of the Three-Way Handshake via TCP/IP

Studying Normal Traffic, Part Three: TCP Headers | Symantec Connect