banner
cells

cells

为美好的世界献上 bug

TCP

TCP/IP Protocol Three Way Handshake#

The three-way handshake is used to establish a TCP connection between the client and the server, ensuring that the connection is reliable and synchronized.

  1. First Handshake: SYN

    • The client sends a SYN (Synchronize) message to the server, indicating a request to establish a connection.
    • The client enters the SYN-SENT state, waiting for the server's response.
    Client - SYN ---> Server
    
  2. Second Handshake: SYN-ACK

    • After receiving the client's SYN message, the server responds with a SYN-ACK (Synchronize Acknowledge) message, indicating acceptance and agreement to establish the connection, while also sending a SYN to the client to request a reverse connection.
    • The server enters the SYN-RECEIVED state.
    Client <--- SYN-ACK - Server
    
  3. Third Handshake: ACK

    • After the client receives the server's SYN-ACK message, it sends an acknowledgment ACK message, indicating that the connection has been established.
    • The client enters the ESTABLISHED state, and the server also enters the ESTABLISHED state after receiving the ACK.
    Client - ACK ---> Server
    

Purpose of the Three Way Handshake#

To ensure that both parties can correctly send and receive data and synchronize their sequence numbers.

Explanation of Why TCP Requires Three Way Handshake#

The three-way handshake is to ensure that both parties have the capability to send and receive (both parties must confirm each other's ability to send and receive).

  • First Handshake: The client tells the server that it can send data.
  • Second Handshake: The server tells the client that it can send and receive data.
  • Third Handshake: The client tells the server that it can receive data.

TCP/IP Protocol Four Way Handshake#

The four-way handshake is used to disconnect the connection between the client and the server. Since TCP is a full-duplex protocol, both parties need to close their sending and receiving channels, thus requiring four handshakes to complete the disconnection operation.

  1. First Wave: FIN

    • The client sends a FIN (Finish) message, indicating that it will no longer send data but can still receive data.
    • The client enters the FIN-WAIT-1 state.
    Client - FIN ---> Server
    
  2. Second Wave: ACK

    • After receiving the client's FIN message, the server sends an ACK message, confirming receipt of the FIN message, but the server may still have data to send.
    • The server enters the CLOSE-WAIT state, and the client enters the FIN-WAIT-2 state.
    Client <--- ACK - Server
    
  3. Third Wave: FIN

    • After the server finishes sending data, it sends a FIN message to the client, indicating that data transmission is complete and it is ready to close the connection.
    • The server enters the LAST-ACK state.
    Client <--- FIN - Server
    
  4. Fourth Wave: ACK

    • After the client receives the server's FIN message, it sends an ACK message, confirming the closure of the connection.
    • The client enters the TIME-WAIT state, waiting for a period (generally 2 * MSL, Maximum Segment Lifetime) before officially closing the connection and entering the CLOSED state. The server immediately enters the CLOSED state after receiving the ACK.
    Client - ACK ---> Server
    

Brief Overview of TCP Congestion Control Algorithms#

  • Slow Start: After establishing a connection, initialize the sending window size, which grows exponentially with each ACK received until it reaches the congestion threshold.
  • Congestion Avoidance: Once the window size reaches the threshold, the sender increases the size by one MSS each time.
  • Fast Retransmit: If the receiver sends 3 duplicate ACKs without waiting for a timeout, the sender immediately retransmits the corresponding segment.
  • Fast Recovery: After entering the fast retransmit phase, the sender halves the window size.

Brief Overview of MTU and MSS#

MTU (Maximum Transmission Unit): The maximum packet size that can be transmitted at the network layer in one go, typically 1500 Bytes for Ethernet.

MSS (Maximum Segment Size): The maximum number of bytes in a single segment that can be sent at the TCP layer, usually MTU minus the TCP/IP header size.

TCP Sticky Packet Problem#

When the client sends multiple packets to the server, the data received by the server's underlying TCP receive buffer is stuck together.

TCP's underlying communication is byte stream oriented and connection-oriented, ensuring the accuracy and order of sent data, with the byte stream being handled byte by byte.

Assuming the TCP buffer size is 10 bytes, when 4 bytes of data ("aaaa") are sent, there are still 6 bytes of free space in the buffer. If 7 bytes of data ("bbbbbbc") are sent again, the first data received by the client is "aaaabbbbbb", with 1 byte of data remaining unsent, waiting for the next send.

Causes of TCP Sticky Packet#

Multiple small packets stick together during transmission, making it impossible for the receiving end to distinguish the boundaries of these packets, resulting in merged data.

  1. (Sending too fast) When the client's sending frequency is much higher than the server's receiving frequency.
  2. (Sending too little) The safety and efficiency mechanisms at the TCP layer do not allow packets with particularly few bytes to be sent too frequently; TCP accumulates to a certain size before sending together. (Nagle)
  3. (Buffer residue) There is data from the previous send that has not been fully sent in the sender's buffer, or there is data in the receiver's buffer that has not been retrieved.

TCP Sticky Packet Handling Methods#

Mainly using application layer defined packet format methods, commonly known as packet cutting processing.

Using TLV Protocol#

TLV (Type Length Value) protocol.

+---------+---------+---------+
|  type   |  length |  value  |
+---------+---------+---------+

Packaged data for sending, unpackaged data for receiving.

Using Delimiters#

Adding special characters between messages.

Setting Fixed-Length Messages#

Specifying the length of messages, allowing the receiver to split based on length.

TCP Implementation of Full Duplex#

  1. Independent sending and receiving buffers, both parties have a sending buffer and a receiving buffer, allowing both parties to send and receive data simultaneously.
  2. Independent sequence numbers and acknowledgment mechanisms, TCP divides the data stream to be sent into multiple segments, each segment's bytes have a unique sequence number, ensuring the position of sent packets within the entire data stream, allowing the receiver to determine the correct order of data and whether there are any lost packets based on sequence numbers.
  3. Flow control, TCP adjusts the sending rate through a sliding window mechanism to ensure that the receiver's buffer does not overflow (be overwhelmed by data).
  4. Congestion control, which prevents network overload by adjusting the sender's sending rate.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.