A directory

What’s the difference between a free front end and a salted fish

directory
A directory
The preface
Three UDP
Four TCP connection
 4.1 Establishing a Connection
  4.1.1 Three-way handshake process
  4.1.2 Question: Won’t two handshakes work?
  4.1.3 QUESTION: Can data be carried during the three-way handshake?
 4.2 Data Transmission phase
 4.3 Disconnection Phase
  4.3.1 Four-wave process
  4.3.2 QUESTION: Why is it necessary to wave four times
Five Differences between TCP and UDP
Vi References

The preface

Returns the directory

Interviewer: Do you know the three-way handshake and the four-way wave? Can you describe the process? Why not shake hands twice? And why does it take four waves to disconnect?

Three UDP

Returns the directory

The User Datagram Protocol (UDP) is a Protocol developed based on IP to communicate with applications.

One of the most important pieces of information in UDP is the port number, which is a number bound to every application that wants to access the network.

UDP sends the specified packet to the specified program by port number, so the IP address information sends the packet to the specified computer, and UDP sends the packet to the correct program by port number.

UDP transmission defects:

  • Data packets are easily lost during transmission
  • In large file transfer,UDPI don’t know how to compose these packets, or how to restore them to full files.

Although UDP does not guarantee data reliability, it is very fast, so UDP will be used in some areas where speed is a concern but data integrity is not so strict, such as online video, interactive games and so on.

Four TCP connection

Returns the directory

Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte stream – based transport layer communication Protocol.

TCP has two characteristics compared with UDP:

  • Establish a retransmission mechanism for lost packets
  • TCPThe packet sorting mechanism is introduced to ensure that out-of-order packets are combined into a complete file

In a TCP connection, there are three procedures.

4.1 Establishing a Connection

Returns the directory

This phase establishes the connection between the client and server through a “three-way handshake.”

TCP provides connection-oriented communication transport. Connection-oriented means that data communication starts with the preparation work between the two ends.

The three-way handshake means that when a TCP connection is established, the client and server send a total of three packets to confirm the connection.

The main functions of the three-way handshake are:

  • Confirm the receiving and sending capabilities of both parties
  • Specify your own initialization sequence number in preparation for later reliability

4.1.1 Three-way handshake process

Returns the directory

At first, the client is in the Closed state and the server is in the Listen state.

  1. The client sends to the server. Client sendSYNThe message is sent to the server and indicates that the client initializes the sequence number asISN(c), i.e., according to theSYN=1, seq=xSend it in the form of. At this point the client is inSYN_SENDState.
  2. The server sends it to the client. The server receives the packet from the clientSYNISN(c)And send one, tooSYNGo back and set at the same timeACK = ISN(c) + 1And specify the server initialization sequence number asISN(s), i.e., according to theThe SYN = 1, ACK = x + 1, seq = yIs sent to the client in the form of
  3. The client sends to the server. Set after the client receives the message from the serverACK = ISN(s) + 1, will ownISN(c) + 1, i.e., according to theACK=y+1, seq=x+1“Is sent to the server. At this point the client is inESTABLISHEDPhase: The server receives the packet and is also inESTABLISHEDPhase, the two sides establish a connection.

Pictures from Zhihu, too lazy to redraw, watermark retention, such as invasion delete

4.1.2 Question: Won’t two handshakes work?

Returns the directory

Purpose of the triple handshake:

  1. The client sends data to the server, and the server confirms that it can accept the client’s request.
  2. The server sends data to the client, and the client confirms that it can send data to the server and accept requests from the server.
  3. The client sends data to the server, and the server confirms that it can send data to the client.

If two handshakes are used, the client sends data to the server, and the server confirms it as a connection:

  1. The client sends a request to the server… No response was sent after the specified time
  2. The server receives the second request to establish a connection, and then the first request to establish a connection is reached after the second because of network latency and so on
  3. The server thinks the second request is new and sends a confirmation message to the client agreeing to establish a connection, so the connection is established (two handshakes)
  4. The client is still waiting for the latest connection request (the second request) and automatically ignores the response the server sends about the first connection request and does not send data
  5. The server is waiting for the client to send data, and the server resources are occupied

In a nutshell:

Client -> 1 -> Server -> 2 -> Server Server GET 1 Server GET 2 Server Reply 1 Client Wait 2 GGCopy the code

That’s why you need three handshakes instead of two.

4.1.3 QUESTION: Can data be carried during the three-way handshake?

Returns the directory

Answer: You can carry it on the third handshake, but not the first and second.

Reason: If data can be carried for the first time, it may be a malicious attack on the server. At this time, a large amount of data is released, regardless of the server’s capacity, so that the server spends a lot of time, memory space to receive packets.

On the third handshake, the client is in the ESTABLISHED state. It can establish a connection and knows that the server’s receiving and sending capabilities are normal, so it can carry data.

4.2 Data Transmission phase

Returns the directory

Both the client and server are in the ESTABLISHED state.

In this phase, the receiver needs to confirm each packet. That is, after receiving the data packet, the receiving end needs to send the confirmation packet to the sending end.

If the sender does not receive the acknowledgement message from the receiver within a specified period of time, the sender determines packet loss and triggers its own retransmission mechanism.

A large file is divided into multiple small packets during transmission. The receiving end sorts the packets according to the sequence number in the TCP header to ensure complete data.

4.3 Disconnection Phase

Returns the directory

After the data transfer is complete, the connection needs to be terminated and the two parties need to be able to disconnect by waving their hands four times.

4.3.1 Four-wave process

Returns the directory

  • The client sends it to the server. The client toFIN=1, seq=uIs sent to the server, indicating that data transmission between the client and the server needs to be stopped. At this point the client is inFIN_WAITState.
  • The server sends it to the client. The server receives the message and returns firstACKTo the client, i.eACK=1, seq=v, ack=u+1Is returned to the client, indicating that the client receives the packet. The server is in theCLOST_WAITState.
  • The server sends it to the client. The server waits to see if the client has any data to send. After processing the data, the server also wants to disconnect and sends the dataFINTo the client, i.eFIN=1, ACK=1, seq=w, ack=u+1Is sent to the client in the form of The server is in theLAST_ACKState.
  • The client sends it to the server. The client receivesFINAfter that, returnACKA packet is sent as a replyACK=1, seq=w+1“Is sent to the server. At this point the client is inTIME_WAITState.

After a while, the client ensures that the server receives its ACK packet, and the state becomes CLOSED. The server is also in the CLOSED state after receiving the ACK packet.

Pictures from Zhihu, too lazy to redraw, watermark retention, such as invasion delete

4.3.2 QUESTION: Why is it necessary to wave four times

Returns the directory

Because the server receives a close request from the client.

If some data is not delivered due to network latency, then it will be directly turned off so that the data is not received; Or the server may have some data to send to the client, and make sure it’s done.

We know that TCP is a reliable guy, so it will reply to the client for the first time to receive the request to close the connection, and the second time to reply to the client, the data you sent should not be delayed, AND I have also sent the data I want to send, so it can be closed.

When the client finally receives it, the reply tells the server that it can also shut down, and then after a while ensures that the server receives the ACK message, it is also CLOSED.

Five Differences between TCP and UDP

Returns the directory

  • TCPIt’s connection-oriented,UDPIt’s connectionless that means you don’t need to establish a link before sending data.
  • TCPProvide reliable service. In other words, passTCPConnect the transmitted data, no error, no loss, no repetition, and arrive in sequence; UDP does its best to deliver, i.e. reliable delivery is not guaranteed. And because ofTCPReliable, connection-oriented, no data loss and therefore suitable for large data volume exchanges.
  • TCPIs a byte stream,UDPPacket oriented, and network congestion does not slow down the transmission rate (hence packet loss, for real-time applications such asIPTelephone and video conferencing, etc.).
  • TCPIt can only be 1 to 1,UDPSupport 1 to 1, 1 to many.
  • TCPIs larger at the head20Byte, andUDPonly8Bytes.
  • TCPIs connection-oriented reliability transmission, whileUDPIs not reliable.

Vi References

Returns the directory

  • Interviewer, stop asking me for three handshakes and four waves【 reading suggestions: 1hour】
  • What is the interviewer trying to ask us about three handshakes and four waves【 Reading Suggestions: 20min】
  • TCP three handshakes and four breakups are understood in colloquial terms【 Reading Suggestions: 20min】
  • TCP protocol details[Recommended Reading: 40min]
  • Have you ever been asked about TCP/IP in an interview?【 Reading Suggestions: 20min】
  • “Three handshakes, four waves.” Do you really understand?【 Reading Suggestions: 20min】

Jsliang’s document library is licensed by Junrong Liang under the Creative Commons Attribution – Non-commercial – Share alike 4.0 International License. Based on the github.com/LiangJunron… On the creation of works. Outside of this license agreement authorized access can be from creativecommons.org/licenses/by… Obtained.