Soul-searching question: Do you fully understand TCP&UDP?

Soul-searching question: Do you fully understand TCP&UDP?

Everyone should have heard of TCP and UDP. Both TCP and UDP work at the transport layer, and their goal is to transfer data between applications. Our common network communications, such as browsing the web, checking emails, and making phone calls, all use these two protocols to transfer data.

01. The difference between TCP and UDP

What is the difference between these two protocols? How do they work? Follow the document below~

What is the core difference between TCP and UDP? You will know it by looking at the picture below.

TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte-stream-based transport layer communication protocol. Reliability is the biggest feature of TCP, which is mainly reflected in: no data loss, no errors, no disorder, and no duplicate arrival. As shown in the figure above, TCP can transmit the "children" (data packets) to the receiver very safely.

UDP (User Datagram Protocol) is a connectionless, unreliable, fast-transmission transport layer communication protocol. Fast transmission is the biggest feature of UDP, which is mainly reflected in: no need to establish a connection in advance before sending data, which can transmit data more efficiently, but reliability cannot be guaranteed. As shown in the figure above, UDP is only responsible for sending the "child" (data packet), regardless of whether the receiver has received it.

After intuitively feeling the difference between TCP protocol and UDP protocol, let's take a look at how TCP protocol works?

02. How does TCP work?

The connection management mechanism (reliable mechanism) of TCP communication can be summarized as: three handshakes to establish a connection and four handshakes to disconnect.

<Three-way handshake to establish a connection>

TCP is a connection-oriented protocol, so each request needs to be confirmed by the other party. The TCP client and the TCP server need to complete a three-way handshake before communicating to establish a connection.

First Handshake

Purpose: This is a data packet requesting to establish a connection. The client first sends a synchronization data packet to the server.

The TCP header content of the data packet:

  • Synchronous SYN=1 (client requests to establish a connection)
  • Sequence number seq=x (the sequence number of the first byte of data sent by the client)

Second handshake

Purpose: After receiving the first data packet sent by the client, the server determines that it is a data packet actively establishing a connection. If the server agrees to connect, the server sends a data packet in response.

The TCP header content of the data packet:

  • Synchronous SYN=1 (server establishes connection)
  • Confirmation flag ACK=1 (the server agrees to the connection)
  • Sequence number seq=y (the sequence number of the first byte of data sent by the server)
  • Acknowledgement number ack = x+1 (x bytes of data sent by the client have been received, and the client is told to start sending from the x+1th byte of data next time)

The third handshake

Purpose: After the client receives confirmation from the server, it sends a data packet to the server.

The TCP header content of the data packet:

  • Synchronous SYN=1 (both parties have agreed to establish a connection)
  • Confirmation flag ACK=1 (acknowledgement packet received from the server)
  • Sequence number seq=x+1 (the data packet sent is the x+1th byte of the data)
  • Confirmation number ack=y+1 (receive y bytes of data sent by the server, and tell the server that it should start sending from the y+1th byte of the data next time)

Tips

In the TCP data transmitted between the client and the server, the values ​​of the sequence number seq and the confirmation number ack of both parties are calculated based on each other's seq and ack values, which ensures the continuity of TCP data transmission. Once the TCP data sent by one party is lost, the "handshake" cannot continue, thus ensuring the smooth completion of the "three-way handshake".

< Wave four times to disconnect >

There is always a time to meet and part. After the data transmission is completed, the TCP client and the TCP server need to disconnect through four sessions. We call this process four waves.

First wave

Purpose: The client sends a connection release request packet to the server and stops sending data.

In the TCP header of the connection release packet:

  • Termination FIN=1 (the client actively releases the TCP connection between the client and the server)
  • Sequence number seq=x (x is specified by the client. Then wait for confirmation from the server)

Second wave

Purpose: After receiving the connection release message, the server sends a confirmation message to the client. Therefore, the connection from the client to the server is released, and the TCP connection is in a half-closed state. Half-closed because the client can no longer send data to the server, and half-open because the server can still send data to the client and be received by it.

In the TCP header of the confirmation message sent by the server to the client:

  • Confirmation mark ACK=1 (the data packet sent by the client has been received, and the client agrees to release the connection)
  • Confirmation number ack=x+1 (based on the client message received, the sequence number seq value is increased by 1 as the value of the confirmation number ack of this segment of the message)
  • Sequence number seq=y (y is specified by the server. Then wait for confirmation from the client)

The third wave

Purpose: If the server has stopped sending data to the client, it releases the connection and sends a confirmation message to the client.

In the TCP header of the confirmation message:

  • Termination FIN=1 (the server releases the TCP connection from the server to the client and no longer sends data to the client)
  • Confirmation mark ACK=1 (the server has completed data transmission to the client)
  • Confirmation number ack=x+1 (based on the client message received, the sequence number seq value is increased by 1 as the value of the confirmation number ack of this segment of the message)
  • Sequence number seq=z (z is assigned by the server. Then wait for confirmation from the client)

The fourth wave

Purpose: After receiving the connection release message segment from the server, the client sends a confirmation message to the server.

In the TCP header of the confirmation message:

  • Termination FIN = 1 (client and server disconnect)
  • Confirmation flag ACK=1 (received the confirmation message from the server and agreed to the server to release the connection)
  • Confirmation number ack=z+1 (based on the message received from the server, the sequence number seq value is increased by 1 as the value of the confirmation number ack of this message)
  • Sequence number seq=x+1 (based on the message received from the server, its confirmation number is used as the value of the sequence number of this message)

After understanding TCP, let's learn how UDP works~

03How does UDP work?

UDP protocol is connectionless, that is, it does not need to establish a connection before sending data (no complicated three-way handshake and four-wave process of TCP protocol). Sending data is simply to encapsulate the data packet and then send it out from the network card. There is no state connection between the data packets. UDP protocol is basically the interface between IP protocol and upper layer protocol. UDP protocol uses the service provided by IP layer to transfer the data obtained by application layer from an application process of one host to an application process of another host.

For example~

When the transport layer receives a UDP datagram from the IP layer, it passes the UDP datagram to the application process through the corresponding port based on the destination port in the UDP datagram header, as shown in the following figure.

As shown in the above figure, application process 4 and port 2, if the receiving UDP finds that the destination port number in the received message is incorrect (that is, there is no application process corresponding to the port number), the message will be discarded and ICMP will send a "port unreachable" error message to the sender.

Tips

ICMP (Internet Control Message Protocol) is a sub-protocol of the TCP/IP protocol suite, used to transmit control messages between IP hosts and routers. Control messages refer to network messages such as whether the network is connected, whether the host is reachable, and whether the route is available.

04Summary

Data transmission seems simple, but it is actually very smart~

TCP and UDP serve programs, but programs are the same as people. They can communicate with each other and talk to each other. TCP is about honest communication between each other. Whether the message is received or not, and whether the received information is consistent, can all be fed back in time.

Advantages: Reliable.

Disadvantages: Slow transmission rate.

Applicable scenarios: Account login, payment and other related functions of communication software, using reliable TCP.

UDP means talking. I don’t care whether you listen to me or not, and whether you give me feedback or not. I will keep talking.

Advantages: fast transmission rate.

Disadvantages: Unreliable.

Applicable scenarios: QQ, WeChat and other instant messaging software usually use UDP with fast transmission speed for point-to-point communication or audio and video calls.

TCP and UDP each have their own advantages and disadvantages, it depends on your actual needs~

For more TCP tips, please click on the three-way handshake and the four-way wave. It turns out that TCP is so polite!

<<:  Edge computing in 5G

>>:  A brief discussion on Bluetooth mesh technology and its application in smart home field

Recommend

RackNerd: $39/month-E5-2690/4GB/120GB/5TB/San Jose & Seattle & Dallas, etc.

The tribe has shared a lot of cheap RackNerd VPS ...

2018 Top Ten Internet Trends Prediction: 5G Becomes the Focus

Whether it is the turbulent forty years in Wu Xia...

China Unicom makes first HD call on 5G SA network

2019 is the first year of 5G commercial deploymen...

The Evolution of Cloud Desktop! The Solution for Future Office

Five or six years ago, the rumor that "cloud...

How to set IP in CentOS9

Just for record, I found that there are still hug...

China Mobile's TD-SCDMA network withdrawal begins: Fujian has taken the lead

[[259267]] Recently, the Fuzhou Radio Management ...

Three common misunderstandings about SD-WAN

Traditional WANs can no longer keep up. In the br...

Starting next year, your home router should be upgraded to Wi-Fi 6!

This article is reproduced from Leiphone.com. If ...