Get answers and suggestions for various questions from here

"Illustration TCP/IP" IV - TCP and UDP



The IP packet header in the network layer has a field for identifying the upper layer of the network layer, that is, which transport protocol is used by the transport layer. According to this field, it can be identified whether it is TCP transmission or UDP transmission; likewise, the transport layer TCP and UDP in order to identify which application the data should be sent to, also set a field identifier, this field identifier is the port number. (Data is an application sent from one host application to another. The IP can find the location of the target host. The port number can be used to determine which application on the target host should be used.)

Communication identification, TCP/IP, UDP/IP communication uses five messages to identify a communication, "source IP address", "destination IP address", "source port number", "destination port number", "transport protocol (TCP) /UDP)". As long as there is one difference, these five can be considered as two different communications.

First, UDP

UDP transport does not provide a complex control mechanism, it uses IP to provide connectionless communication services. UDP packets do not have reliability guarantee, sequence guarantee, and flow control. The reliability is relatively poor. Even if packet loss occurs during transmission, it will not be retransmitted. It is also because of this, its resource consumption is small, the transmission speed is fast, usually audio, video is transmitted by UDP when transmitting.

Second, TCP

TCP is a connection-oriented transport protocol that only sends data when it is confirmed that the communication peer exists, so that it can control the waste of communication traffic. At the same time, TCP also fully realizes various control in the process of data transmission, which can be retransmitted when data is lost, and can also be sequentially controlled for sub-packets that are out of order.

Third, the characteristics of TCP

The introduction of TCP here does not describe some of the characteristics of TCP and the implementation process of the transmission. The main reason is to understand a process of TCP transmission. The specific implementation does not have a deep understanding.

3.1, connection management

UDP is a connection-oriented transport protocol. It sends data directly without checking whether the peer exists. TCP is a connection-oriented transport protocol. Before data transmission, the two ends of the communication are established.

The client sends a SYN packet through the TCP header as a request to establish a connection, and waits for the server to confirm the response. If the peer sends an acknowledgment response, it considers that communication can be performed. If the client does not receive the acknowledgment response from the server, the client does not communicate. . After the communication ends, the client will also send a FIN packet to disconnect the connection. The establishment and disconnection of a connection, normally need to send seven packets back and forth to complete.

establish connection:

  • The client sends a SYN packet to the server. At this point, the client enters the SYN_SEND state and waits for the server to confirm.
  • After receiving the SYN packet sent by the client, the server must confirm the SYN packet of the client, and also send a SYN packet to the client, that is, ACK+SYN. At this time, the server enters the SYN_RECEIVED state.
  • After receiving the ACK+SYN packet of the server, the client sends an acknowledgement packet ACK to the server. After the packet is sent, the client and the server enter the ESTABLISHED state and complete the three-way handshake.


  • The client sends a FIN packet to the server to close the client-to-server data transfer.
  • When the server receives the FIN packet, it sends an ACK confirmation response to the client.
  • The server closes the connection with the client and sends a FIN packet to the client.
  • After receiving the client, the client sends an ACK response to the server, and the connection is disconnected.

3.2. Improve transmission reliability by serial number and confirmation response

The TCP transmission protocol starts data transmission after the two hosts establish a connection. When the data of the sender arrives at the receiver, the receiver returns a response to the received message. This message is called acknowledge acknowledgement ACK. TCP implements reliable data transmission by acknowledging the acknowledgement ACK.

After the sender sends the data, it will wait for the acknowledgement from the other party. If there is an acknowledgement, the data has successfully arrived at the peer. Otherwise, if the sender does not receive the ACK response within a certain period of time, the data is considered to be lost and will be sent again. .

The serial number is the number of each byte of the transmitted data in the order, and the receiving end sends the serial number of the data that should be received next time as the confirmation response to the serial number and the data length in the header of the data packet. The sender, in this way, TCP can achieve reliable transmission by serial number and acknowledgment response number.