Soul torture: Do you fully understand TCP&UDP?

2023.06.24

Soul torture: Do you fully understand TCP&UDP?

Our commonly used network communications, such as browsing the web, checking emails, and telephone communications, all use these two protocols for data transmission.

Everyone should have heard about the TCP protocol and the UDP protocol. Both the TCP protocol and the UDP protocol work at the transport layer, and their goals are to transmit data between applications. Our commonly used network communications, such as browsing the web, checking emails, and telephone communications, all use these two protocols for data transmission.

picture

01. The difference between TCP and UDP

What is the difference between these two agreements? How do they work? Follow the document gentleman to look down~

What is the core difference between the TCP protocol and the UDP protocol? Take a look at the picture below and you will know.

picture

TCP (Transmission Control Protocol, Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer communication protocol. Reliability is the biggest feature of TCP, mainly reflected in: no data loss, no error, no disorder, no repeated arrival. Just like the picture above, TCP can transmit "children" (data packets) to the receiver very safely.

UDP (User Datagram Protocol, User Datagram Protocol) is a connectionless, unreliable, and fast-transmission transport layer communication protocol. Fast transmission is the biggest feature of UDP, which is mainly reflected in: before the data is sent, there is no need to establish a connection in advance, and the data can be transmitted more efficiently, but the reliability cannot be guaranteed. Just like the picture above, UDP is only responsible for sending out "children" (data packets), regardless of whether the receiver receives them or not.

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

02. How does TCP work?

The connection management mechanism (reliable mechanism) of TCP communication is 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 the three-way handshake before communicating to establish a connection.

picture

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 transmitted by the client)

second handshake

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

The TCP header content of the data packet:

  • Synchronous SYN=1 (the server establishes a connection)
  • Confirmation mark ACK=1 (server agrees to connect)
  • Sequence number seq=y (the sequence number of the first byte of data transmitted by the server)
  • Confirmation number ack=x+1 (x bytes of data sent by the client have been received, and the client should be sent from the x+1th byte of the data next time)

third handshake

Purpose: After the client receives the 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 mark ACK=1 (acknowledgment packet received from the server)
  • Serial number seq=x+1 (the sent data packet 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 to 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 on the basis of each other's seq and ack values, which ensures the continuity of TCP data transmission. Once the TCP data sent by a certain party is lost, the "handshake" cannot be continued, thus ensuring the smooth completion of the "three-way handshake".

< four times wave to disconnect >

At the end of the gathering, sometimes, after the data transmission is completed, the TCP client and the TCP server need to disconnect through four sessions. This process is called four waved hands.

picture

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:

  • Terminate FIN=1 (the client should actively release the TCP connection between the client and the server)
  • Serial number seq=x (x is specified by the client. Then wait for the confirmation of the server)

second wave

Purpose: After the server receives the connection release message, it sends a confirmation message to the client. Therefore, the connection in the client-to-server direction is released, and the TCP connection is now 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 is received, and the client is agreed to release the connection)
  • Confirmation number ack=x+1 (on the basis of receiving the client message, add 1 to its serial number seq value as the value of the confirmation number ack of this segment of the message)
  • Serial number seq=y (y is specified by the server. Then wait for the client's confirmation)

third wave

Purpose: If the server has stopped sending data to the client, release the connection and send a confirmation message to the client.

In the TCP header of the confirmation message:

  • Terminate FIN=1 (the server releases the TCP connection from the server to the client, and no longer sends data to the client)
  • Acknowledgment mark ACK=1 (the data transmission from the server to the client is completed)
  • Confirmation number ack=x+1 (on the basis of receiving the client message, add 1 to its serial number seq value as the value of the confirmation number ack of this segment of the message)
  • Serial number seq=z (z is specified by the server. Then wait for the confirmation of the client)

fourth wave

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

In the TCP header of the confirmation message:

  • terminate FIN = 1 (client and server disconnect)
  • Confirmation mark ACK=1 (receive the confirmation message from the server and agree to release the connection from the server)
  • Acknowledgment number ack=z+1 (on the basis of receiving the server message, add 1 to its serial number seq value as the value of the acknowledgment number ack of this segment of the message)
  • Serial number seq=x+1 (on the basis of receiving the server message, use its confirmation number as the value of the serial number of this segment of the message)

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

03How does UDP work?

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

Take a chestnut~

When the transport layer receives the UDP data packet from the IP layer, it transmits the UDP data packet to the application process through the corresponding port according to the destination port in the header of the UDP data packet, as shown in the figure below.

picture

As shown in the figure above, the application process 4 and port 2, if the receiver 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 sent by ICMP "Port unreachable" error message to the sender.

tips

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

04 Summarize

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

TCP and UDP serve programs, but programs are the same as people. You can communicate with each other, or you can talk to each other. TCP is all about honest communication with each other. Whether the message has been received, and whether the received information is consistent, can have timely feedback.

picture

Pros: Reliable.

Disadvantage: slow transfer rate.

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

UDP is talking, I don't care if you listen or not, whether you have feedback or not, I will keep talking.

picture

Advantages: fast transfer rate.

Cons: Unreliable.

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

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

For more TCP dry goods, please poke the three-way handshake and wave four times, it turns out that TCP is so polite!