According to my understanding, why TCP shakes hands three times instead of two.
Step by step.
1. TCP is reliable
TCP is a reliable transmission. Handshake refers to TCP establishing link. How can we establish link reliably?
2. The simplest way is to establish a reliable transmission channel
A wants to send data to B. if there is no link, it will be sent directly. So at least build a link.
A sends a request to establish a link first, and B tells a “I received it” after receiving it. So you need a to send once and B to send once to make sure that a sends to B is connected.
Is this really reliable? It’s also possible that after B tells a “I received it”, the channel is blocked, and then sends data to B, but B can’t receive it, but this situation is rare. TCP has other mechanisms to know this situation.)
3. How many packets does AB need to send to establish a link with each other
From 2, we know that a needs to send two packages to a once to establish a link from a to B, and B needs to send two packages to a once, so AB needs to establish a link to each other four times,howeverB tells a that it has received a request from B to establish a link with A. these two packages can be combined into one, so it takes three times
4. Is it OK for AB to link two packages?
As we know from 2 and 3, two handshakes of two packets can only ensure the reliability of a single channel. TCP is duplex and can send and receive packets when mutual confirmation is required. Therefore, two handshakes are not allowed.
5. Formal process
- A requests B to establish a link, the syn flag is marked, and the SEQ is filled with X.
- B receives the establishment request from a, and then sends syn flag, ACK flag, ACK = x + 1, SEQ = y to a
- A receives B’s receive packet and establish request packet, and then sends ack flag to indicate receiving syn packet, ACK = y + 1, SEQ = x + 1
Personal profile: Senior Development Engineer, interests and fields (unity, unreal, Cocos creator, Android terminal development, IOS terminal development, audio and video development, graphics), welcome to discuss problems with W: wlxklyh（ Welcome Star:https://github.com/wlxklyh/SoftRenderer）