Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Word-wrap: break-word! Important; "> < p style =" max-width: 100%; Is everybody interview time had been asked 😀 > how is everybody answer at that time? 🤔 > In addition, the collation and summary of this article if there is any problem welcome big brothers to point out!Copy the code

The answers in this article are referenced

  • Kobayashi’s Illustrated Network
  • Geek time interesting talk about network protocols
  • RFC793
  • Zhihu god’s answer

Thank you for sharing your knowledge!

In addition, the following content is also mixed with a lot of personal understanding if you have other ideas welcome to exchange!! Thank you!!!!!

Interviewer: “Let’s talk about computer networks and why TCP connections have three handshakes instead of two/four?”

Me: “…”

This is a classic question

  • I’ve seen the network diagram before

  • Xie xiren’s computer network corresponds to this section

  • I have also searched zhihu and read the content of the RFC interpreted by the big guy

First, there are two conclusions.

  • Avoid repeated connectionsWaste of resources
    • Prevent invalid connection request packets from being sent to the server after a period of time
  • Avoid historical connectionsNetwork chaos
    • Three-way handshake prevents historical connections (by sending RST messages)
    • TCP is reliable transmission and requires a three-way handshake to agree and determine the initial sequence numbers of both parties.

Note that RFC 793 explains why TCP connections use the three-way handshake in the first place.

> The principle reason for the three-way handshake is to > prevent old duplicate connections from causing confusion. Avoid network clutter caused by old duplicate connections (historical connections)!Copy the code

So before WE talk about those two things let’s talk a little bit about the three handshakes that make a connection and draw a little sketch

The following picture of the message structure is from The Diagram Network

SYN packet with the first handshake

Handshake The SYN+ACK packet sent by the server for the second time

Handshake the ACK packet sent by the client for the third time

[1] Avoid the waste of resources caused by repeated connections

Then talk about the performance improvement of three handshakes over two handshakes and some key effects!

Point 1 :(refer to xie xiren’s computer network – avoid the impact of broken connections – kobayashi’s diagram of networks – avoid waste of resources)

  • The first connection request segment sent by the client is not lost but is blocked at a network node for a long time.

  • What happens if you shake hands twice?

    • So the package becomes an unwanted presence whereas the server will not know that it is a duplicate network package if it uses a two-handshake! (Two handshakes)
    • After some delay the old connection request arrives at the server and the server mistakenly thinks it is a new connection request sent by the client (two handshakes)
    • The server then sends a confirmation message to the client and establishes a new connection (two handshakes)
    • So using two handshakes leads to a huge waste of resources!
  • Three handshakes are much more witty!

    • In order to establish a connection, the client must send the final ACK message to the server (ACK number: server_sin + 1)

[2] Avoid network chaos caused by historical connections

Sounds like The Quet is more terrible than the Internet chaos lol

Second: [primary reason] The triple handshake is used to determine the initial serial number of both sides. (The triple handshake is used to synchronize the initial serial number of client and server. Avoid historical connections)

  • Due to network congestion and other chaotic reasons, the old packets will reach the target host first

    • The three-way handshake prevents confusion caused by old duplicate connection initializations

    Refer to RFC 793 for the primary reason to use the three-way handshake for TCP connections

    The principle reason for the three-way handshake is to prevent old duplicate connections from causing confusion.

    That is, to prevent the network from being messed up by the initialization of old duplicate connections

    Use one of kobayashi’s graphic networks to illustrate the problem

    • When the old data packet arrives at the server before the new one, the client sends an RST message telling the server, “This is my historical connection. Do not connect and allocate resources.”
  • And an Ali engineer on Zhihu (which is not quite the same as in the illustrated network!) said so. The three-way handshake can determine the initial SEQ sequence number (ISN) of the client and server, thus preventing confusion caused by historical connections

    • TCP needs to reliably retransmit or receive the SEQ sequence number so that the connection reuse cannot distinguish the SEQ as delayed/old connection SEQ
    • The two-handshake ensures that the initial sequence number (that is, the sequence number in SQN packets) of one party is successfully received by the other party
      • Attention! Only once (one side sends a SYN and the other side sends an ACK to confirm that the SYN has been received successfully!) That’s success.
    • A three-way handshake ensures that both parties’ initial sequence numbers are accepted so that no historical connection can be corrupted!

Some references are below

A comment on Geek Time

Application layer packet -> (through socket programming to achieve packet delivery ->) Transport layer payment this scenario often uses the TCP protocol in the transport layer. TCP will ensure that the packet can reach the destination, if not, it will be re-sent until it arrives

It’s like writing a letter, putting it in an envelope, looking up the address of the destination from the address book (DNS) based on the addressee’s name (web address) zip code (IP), and sending it to the post office (transport layer) for transmission, with some twists and turns.

1. You say: dear so-and-so, let’s write to each other in the future (SYN=1 initiate a new connection). I have a secret I can’t tell you, but I’m not sure if it was you who received this letter.

I want to write to you too (SYN=1 initiates a new connection). I am myself and you can write to me now (ACK=1 confirms that the sequence number is valid). You can start with the unspeakable secret and move on (ACK= x+1).

We finally got in touch (ACK=1 confirms that the serial number is valid). We went on to my secret…… (seq=x+1), what do you have to say to me next time you write and tell me (seq=y+1) this is the TCP three-way handshake

Answer in Zhihu

** SEQ (sequence numbers) ** is used to establish reliable TCP connections. — From Zhihu

  • Since there was no network global clock, the two machines proved that the packet was new and not delayed in the link (also the second day point of the content in Xiaolin) in order to confirm the serial number, respectively — from Zhihu

Xie Xiren version of computer network —

Invalid connection request segment Is generated in this case: The first connection request segment sent by the client is not lost, but is detained on a network node for a long time. As a result, it is delayed until a certain time after the connection is released. Originally, this is an invalid packet segment. However, after the server receives the invalid connection request packet segment, it mistakenly thinks it is a new connection request sent by the client. Then the client sends a confirmation message to agree to establish a connection. Assuming that the “three-way handshake” is not used, a new connection is established as soon as the server sends an acknowledgement. Since the client does not send a connection request, it ignores the server’s confirmation and does not send data to the server. However, the server assumes that the new transport connection has been established and waits for data from the client. As a result, many of the server’s resources are wasted. The three-way handshake prevents this from happening. For example, the client does not issue an acknowledgement to the server’s acknowledgement. When the server receives no acknowledgement, it knows that the client has not requested a connection.”

In case an error occurs when an invalid connection request segment is suddenly sent to the server

Zhihu blogger Ali, an engineer, thinks this “can only be a superficial cause, not the essence. “So it was proposed —

If you read RFC793, the TCP protocol RFC, you’ll see why the three-way handshake is necessary

TCP requires the SEQ sequence number for reliable retransmission or reception. In order to avoid connection reuse, it is impossible to distinguish whether SEQ is delayed or the SEQ of an old link. Therefore, a three-way handshake is required to determine the ISN (initial SEQ sequence number) of both parties.

Author: ali engineer links: www.zhihu.com/question/24…