Why TCP protocol is reliable

Background problem

  • Daily interviews, almost all studied computer knows, TCP is a reliable protocol, UDP protocol is not reliable. Why TCP protocol is reliable? It is what mechanism to ensure reliable it?

Ask a question

  1. Since MTU IP packets have length limitations when TCP segment is too large to be cut. After cutting to send out, due to the uncertainty of the network link, the receiver receives the packet transmission order and the order is inconsistent with high probability. How the receiving end received "the same group" TCP segment data spliced ​​into the desired binary data?
  2. The sender sends a TCP packet, how to confirm that the recipient receives this message?
  3. The sender sends a TCP packet, how to ensure the receiving end of a data segment received is not "someone else" tampered with on the way?

Entry-level solution TCP protocol

  1. The serial number of a TCP packet identifier
    • TCP segment header, there is a sequence number field (Sequence)
    • Receiving terminal sequence may be used for different TCP packets and sort splicing
  2. Primary Solution: ACK mechanism acknowledgment number +
    • After the recipient receives the sequence number seq of the TCP packet, the sequence number expected to be received TCP packet seq + 1, the number is confirmed filling seq + 1
    • ACK bit is set to 1 to indicate acknowledgment TCP packet received
    • A timeout ACK is not received, the sender that the default TCP segment is lost, a retransmission again
  3. checksum TCP packet header field testing and inspection segment data has been received checksum and the actual segment data consistency. Inconsistency is discarded

The primary problem solution

For the Primary Solution 2: There are two problems
of low efficiency 1..
The sender keep sending -> wait for ACK -> Send -> wait for ACK ... single-line work, such work is called stop-and-wait. stop-and-wait though to achieve the reliability of TCP communication, but at the expense of the efficiency of network communications. Meanwhile, in the period of waiting for an ACK, the network is idle (IDLE) state

2. little defect
if the current TCP segment has not been confirmed, it will lead to subsequent fragments can not be sent out.

TCP protocol optimization

  • Sliding window mechanism
    • Do not expand, you can see the reference links in the sequence of pictures, very detailed
    • TCP segment header with a window
    • Reducing the flow receiving end ACK reply consumed, because the use of the cumulative ACK reply
    • The transmitting end will not be a single TCP segment ACK is not received continues to transmit is blocked in most cases
    • A confirmation number and details seq
      • After receiving the sequence number of the received TCP packets seq, ACK number of acknowledgment packets are not necessarily transmitted seq + 1, may be seq + n (n-1 is the received sequence number of the TCP segment seq before the number) of the TCP segment has been received

to sum up

  • TCP protocol uses the following mechanisms to ensure reliable
    • TCP segment header sequence field uniquely identifies a TCP segment, but also as the identity of the order
    • Communication using TCP acknowledgment number of the acknowledgment mechanism ACK +
    • checksum checksum field in the TCP packet header record segment data
  • Optimized TCP protocol
    • Sliding window mechanism, improve the communication efficiency

Reference material

Guess you like

Origin www.cnblogs.com/HelloGreen/p/11747269.html