This is the 10th day of my participation in Gwen Challenge


The introduction

Network protocols are the knowledge that every front-end engineer must master. TCP/IP has two representative transport layer protocols, namely TCP and UDP. This article introduces these two protocols and the differences between them.

First, TCP/IP network model

For computers and network devices to communicate with each other, they must be based on the same method. For example, rules on how to detect a communication target, which side initiates the communication first, which language is used for communication, and how to end the communication need to be determined in advance. Communication between different hardware, operating systems, all of this requires a rule. This rule is called a protocol.

TCP/IP is a general name of various Internet-related protocol families, such as TCP, UDP, IP, FTP, HTTP, ICMP, and SMTP.

The TCP/IP model is the foundation of the Internet. It is the general name of a series of network protocols. These protocols can be divided into four layers: link layer, network layer, transport layer and application layer.

  • Link layer: encapsulates and unencapsulates IP packets and sends and receives ARP/RARP packets.
  • The network layer is responsible for routing and sending packet packets to the target network or host.
  • Transport layer: groups and reorganizes packets and encapsulates them in TCP or UDP format.
  • Application layer: responsible for providing applications to users, such asHTTP, FTP,Telnet, DNS, SMTP, etc.

In the network architecture, the establishment of network communication must be carried out in the peer layer of the communication parties, not interleaved. In the whole process of data transmission, the protocol header and protocol tail of the corresponding layer must be attached when the data passes through each layer at the sending end (only the protocol tail needs to be encapsulated at the data link layer), that is, the data must be encapsulated by protocol to identify the communication protocol used by the corresponding layer. TCP/IP has two representative transport layer protocols —-TCP and UDP.

Second, the UDP

UDP is a connectionless protocol used to process data packets like TCP on the network. In the OSI model, layer 4, the transport layer, is one layer above the IP protocol. UDP does not provide packet grouping, assembly, and sorting. That is, after a packet is sent, it is impossible to know whether the packet arrived safely and intact.

It has the following characteristics:

1. Connectionless

First of all, UDP does not need to establish a connection by three handshakes like TCP before sending data, and you can start sending data. They are only porters of data packets and do not split or splice data packets.

To be specific:

  • At the sending end, the application layer passes the data to the UDP protocol at the transport layer. UDP simply adds a UDP header to the data to identify the UDP protocol, and then passes the data to the network layer
  • At the receiving end, the network layer passes the data to the transport layer, and UDP passes the IP header to the application layer without any concatenation

2. Unicast, multicast, broadcast function

UDP not only supports one-to-one transmission, but also one-to-many, many-to-many, and many-to-one transmission modes. That is, UDP provides unicast, multicast, and broadcast functions.

3. UDP is packet oriented

After the header is added, the UDP packets from the sender are sent to the IP layer. UDP does not merge or split packets from the application layer, but retains the boundaries of the packets. Therefore, the application must select a message of the appropriate size

4. Unreliability

First of all, the unreliability is reflected in the absence of connection. Communication does not need to establish a connection. If you want to send, it is definitely not reliable.

The data is transmitted as it is received, without backing up the data, and without caring whether the data has been correctly received.

Again, the network is up and down, but UDP always sends data at a constant speed because it has no congestion control. Even if the network condition is not good, the sending rate will not be adjusted. The disadvantage of this implementation is that it may lead to packet loss in the case of poor network conditions, but the advantage is also obvious. In some real-time demanding scenarios (such as teleconference), UDP is used instead of TCP.

As can be seen from the above dynamic diagram, UDP only throws the intended data packets to the other party, regardless of whether the data arrives safely and intact.

5. The header cost is small and the transmission of data packets is very efficient.

The UDP header contains the following data:

  • Two hexadecimal port numbers, source port (optional field) and destination port
  • The length of the entire data packet
  • Checksum of the entire data packet (IPv4 optional field). This field is used to find errors in header information and data

Therefore, UDP header overhead is low, only 8 bytes, compared to TCP at least 20 bytes is much less, in the transmission of data packets is very efficient

Third, the TCP

When one computer wants to communicate with another, the communication between the two computers needs to be smooth and reliable in order to send and receive data correctly. For example, when you want to view a web page or check your email, you want to view the page completely and sequentially without losing anything. When you download a file, you want to get the whole file, not just a part of the file, because if the data is missing or out of order, that’s not what you want, so you use TCP.

Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte stream based transport layer communication protocol defined by RFC 793 of the IETF. TCP is a connection-oriented, reliable streaming protocol. A stream is an uninterrupted data structure, which you can think of as the flow of water in a drainpipe.

1. TCP connection process

As shown in the figure below, you can see that the process of establishing a TCP connection is (three-way handshake process) :

First handshake

The client sends a connection request packet segment to the server. The paragraph contains its own initial data communication sequence number. After the request is SENT, the client enters the SYN-sent state.

Second handshake

After the server receives the connection request packet, if the server agrees to the connection, it sends a response containing its initial data communication number. After the response is complete, the server enters the SYN-received state.

Third handshake

When the client receives a connection approval reply, it sends an acknowledgement message to the server. After sending the packet, the client enters the ESTABLISHED state. After receiving the response, the server also enters the ESTABLISHED state.

Why does TCP require three handshakes to establish a connection, instead of two?

This is because it prevents the invalid connection request message segment from being received by the server, resulting in an error.

2. TCP disconnects

TCP is full-duplex. When the connection is disconnected, both ends need to send FIN and ACK packets.

First handshake

If client A considers the data transfer complete, it needs to send A connection release request to server B.

Second handshake

After receiving the connection release request, USER B tells the application layer to release the TCP connection. The ACK packet is then sent and the state CLOSE_WAIT is entered. In this case, the connection from A to B is released and the data sent by A is no longer received. But because the TCP connection is bidirectional, B can still send data to A.

Third handshake

If there is still uncompleted data sent by USER B, user B will send A connection release request to user A. Then, user B enters the last-ACK state.

The fourth handshake

After receiving the release request, user A sends A confirmation reply to user B. User A enters the time-wait state. The state lasts for 2MSL (maximum segment lifetime, which refers to the duration of the packet segment in the network. The timeout will be discarded). If there is no resending request from B within this period, the state is CLOSED. When B receives the confirmation reply, it also enters the CLOSED state.

3. TCP features

  • connection-oriented

    Connection-oriented means that the connection must be established at both ends before data is sent. The method of establishing a connection is the “three way handshake”, which will establish a reliable connection. The establishment of a connection is to lay a foundation for reliable transmission of data.

  • Only unicast transmission is supported

Each TCP transmission connection has only two endpoints for point-to-point data transmission. Multicast and broadcast transmission modes are not supported.

  • Word oriented stream

Unlike UDP, TCP transmits packets in byte stream mode without preserving packet boundaries.

  • Reliable transport

    For reliable transmission, error codes are determined by the TCP segment number and confirmation number. To ensure the reliability of packet transmission, TCP assigns a serial number to each packet, and the serial number also ensures that the packets sent to the receiving entity are received in sequence. The receiving entity then sends back an acknowledgement (ACK) of the successfully received byte; If the sending entity does not receive acknowledgement within a reasonable round trip delay (RTT), the corresponding data (if lost) will be retransmitted.

  • Provide congestion control

When the network is congested, TCP can reduce the rate and quantity of data injected into the network to alleviate the congestion

  • TCP provides full duplex communication

TCP allows applications on both sides of the communication to send data at any time because there are caches at both ends of the TCP connection to temporarily store two-way communication data. Of course, TCP can send a segment immediately, or it can cache for some time to send more segments at once (the maximum segment size depends on MSS)

Comparison of TCP and UDP

1. The contrast

UDP TCP
Whether connection There is no connection connection-oriented
reliable Unreliable transmission, not using flow control and congestion control Reliable transmission, using flow control and congestion control
Number of connected objects Supports one-to-one, one-to-many, many-to-one and many-to-many interactive communication It can only be one-to-one communication
transport For a message Word oriented stream
The first overhead The header overhead is small, only 8 bytes Minimum 20 bytes, maximum 60 bytes
Applicable scenario For real-time applications (IP phone calls, video conferencing, live streaming, etc.) Suitable for applications that require reliable transmission, such as file transfer

2. To summarize

  • TCP provides connection-oriented reliable services to the upper layer, while UDP provides connectionless unreliable services to the upper layer.
  • Although UDP is not as accurate as TCP transmission, it can also be used in many places where real-time requirements are high
  • TCP can be used if the data accuracy is high and the speed is relatively slow