The introduction
Network protocols are a must for every front-end engineer. There are two representative transport layer protocols in TCP/IP, TCP and UDP. This article will introduce the two protocols and the differences between them.
To read more quality articles, please clickMaking a blog
First, TCP/IP network model
For computers and network equipment to communicate with each other, they must do so in the same way. For example, rules about how to detect a communication target, which side initiates the communication first, which language to use, and how to end the communication need to be determined in advance. Communication between different hardware, operating systems, all of this requires a set of rules. We call this rule a protocol.
TCP/IP is the general name of all kinds of protocols related to the Internet. For example, TCP, UDP, IP, FTP, HTTP, ICMP, SMTP and other protocols belong to the TCP/IP family.
The TCP/IP model is the foundation of the Internet. It is a general term for 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.
- Network layer: routes and sends packets to the target network or host.
- Transport layer: Groups and reassembles packets and encapsulates them in TCP or UDP format.
- Application layer: Responsible for providing applications to users, such as HTTP, FTP, Telnet, DNS, SMTP, etc.
Second, the UDP
User Datagram Protocol (UDP) is a connectionless protocol used to process data packets on the network, just like TCP. In the OSI model, the fourth layer, the transport layer, is one layer above the IP protocol. UDP has the disadvantage of not providing packet grouping, assembly, and sorting, which means that once a packet is sent, it is impossible to know whether it arrived safely and intact.
It has the following characteristics:
1. Connectionless
First of all, UDP does not require a three-way handshake to establish a connection as TCP does. You can send data when you want. In addition, it is only a carrier of data packets and does not split or splice data packets.
Specifically:
- At the sending end, the application layer passes the data to the UDP protocol at the transport layer, which only adds a UDP header to the data and then passes it to the network layer
- At the receiving end, the network layer passes the data to the transport layer. UDP only removes the IP packet header and passes the data to the application layer without any splicing operation
2. It has the functions of unicast, multicast and broadcast
UDP not only supports one-to-one transmission mode, but also supports one-to-many, many-to-many, and many-to-one modes. In other words, UDP provides unicast, multicast, and broadcast functions.
3.UDP is packet-oriented
The UDP packet sent by the sender to the application program adds the header and delivers the packet to the IP layer. UDP does not merge or split the packets sent by the application layer, but retains the packet boundaries. Therefore, the application must select the appropriate size of the packet
4. Unreliability
First of all, unreliability is reflected in no connection, communication does not need to establish a connection, want to send, such a situation is certainly not reliable.
The data is passed as it is received, without backing up the data, and without caring whether the data has been received correctly.
Again, the network environment is up and down, but UDP, because there is no congestion control, will always send data at a constant speed. Even if the network is not good, the transmission rate will not be adjusted. The disadvantage of this implementation is that it may cause packet loss in the case of poor network conditions, but the advantage is also obvious. In some real-time scenarios (such as teleconference), it is necessary to use UDP instead of TCP.
5. The header overhead is small, and the data packet transmission is very efficient.
- Two 16-bit port numbers, the source port (optional field) and the destination port
- The length of the entire data packet
- The validation sum of the entire data packet (IPv4 optional field), which is used to detect errors in the header and data
Therefore, the header overhead of UDP is small, only eight bytes, compared to the TCP at least 20 bytes, 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 so that data can be sent and received correctly. For example, when you want to view a web page or check an email, you want to view the web page completely and sequentially, without losing anything. When you download a file, you want to get the whole file, not just parts of the file, because if the data is lost or out of order, that’s not what you want, so TCP is used.
Transmission Control protocol (TCP) is a connection-oriented, reliable, byte stream based transport layer communication protocol, defined by THE IETF RFC 793. TCP is a connection-oriented, reliable streaming protocol. A flow is an uninterrupted data structure, and you can think of it as a flow of water in a drainpipe.
1.TCP connection process
As shown in the following figure, you can see the process of establishing a TCP connection (three-way handshake process) :
The first handshake
The client sends a connection request packet segment to the server. The packet contains its own initial data communication serial number. After the request is SENT, the client enters the SYN-sent state.
Second handshake
After receiving the connection request packet segment, if the server agrees to connect, it sends a reply containing its own initial serial number. After the reply is complete, the server enters syn-received state.
The third handshake
When the client receives the connection consent reply, it also sends an acknowledgement packet to the server. The client enters the ESTABLISHED state after sending the packet segment, and the server enters the ESTABLISHED state after receiving the reply. In this case, the connection is ESTABLISHED.
Here’s a question you might wonder: Why does TCP require three handshakes to establish a connection, but not two? This is to prevent errors in the case of invalid connection request segments being received by the server.
2. The TCP connection is disconnected
The first handshake
If client A thinks the data is sent, it needs to send A connection release request to server B.
Second handshake
After receiving the connection release request, B tells the application layer to release the TCP connection. It then sends an ACK packet and enters the CLOSE_WAIT state, indicating that the connection between A and B has been released and no more data will be received from A. But because the TCP connection is two-way, B can still send data to A.
The third handshake
If there is still data to be sent, B will continue to send the data. After the data is sent, B will send A connection release request to A. Then B enters the last-ACK state.
The fourth handshake
After receiving the release request, user A sends A confirmation reply to user B. In this case, user A enters the time-wait state. The state lasts for 2MSL (maximum segment duration). If no resend request from B is received within this period, the system enters the CLOSED state. After RECEIVING the acknowledgement, B also enters the CLOSED state.
3. Features of TCP
- connection-oriented
Connection-oriented: a connection must be established between the two ends before data can be sent. The way to establish a connection is to use a “three-way handshake” to establish a reliable connection. Establishing a connection lays the foundation for the reliable transmission of data.
- Only unicast transmission is supported
Each TCP transmission connection has only two endpoints and supports only point-to-point data transmission. Multicast and broadcast transmission modes are not supported.
- Byte oriented stream
Unlike UDP, TCP does not transmit individual packets independently. Instead, TCP transmits packets as a byte stream without preserving packet boundaries.
- Reliable transport
For reliable transmission, the detection of packet loss and error code depends on the TCP segment number and acknowledgement number. To ensure reliable transmission of packets, 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 order. The receiving entity then sends back an acknowledgment (ACK) that the byte has been successfully received; If the sending entity does not receive an acknowledgement within a reasonable round trip delay (RTT), the corresponding data (assuming it is lost) will be retransmitted.
- Provides congestion control
When the network is congested, TCP can reduce the rate and amount of data injected into the network to ease congestion
- TCP provides full-duplex communication
TCP allows applications on both sides of the connection to send data at any time, because both sides of the TCP connection have caches that temporarily store data from two-way communication. Of course, TCP can send a data segment immediately, or it can cache for a period of time to send more data segments at once (the maximum data segment size depends on the MSS)
Comparison between TCP and UDP
1. The contrast
UDP | TCP | |
---|---|---|
Whether connection | There is no connection | connection-oriented |
reliable | Unreliable transmission, no use of flow control and congestion control | Reliable transmission, using flow control and congestion control |
Number of connected objects | Support one-to-one, one-to-many, many-to-one and many-to-many interaction communication | Only one to one communication |
transport | For a message | Byte oriented stream |
The first overhead | Header overhead is small, only 8 bytes | The header has a minimum of 20 bytes and a maximum of 60 bytes |
Applicable scenario | Suitable for real-time applications (IP phone, video conference, live broadcast, etc.) | Suitable for applications that require reliable transfers, such as file transfers |
2. To summarize
- TCP provides connection-oriented and reliable services to the upper layer, while UDP provides connectionless and 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
- For those with high requirements for data accuracy and relatively slow speed, TCP can be used
I recommend a good BUG monitoring toolFundebug, welcome to the free trial!
Welcome to pay attention to the public number: front-end craftsman, your growth we witness together! If you’re feeling rewarded, you’re welcome to give me a tip to motivate me to produce more quality open source content
Refer to articles and books
- Computer network tutorial and simulation experiment
- Differences between TCP and UDP
- The front end of the interview
- An article to understand TCP/IP, TCP, UDP, IP, Socket relationship
- Deep understanding of computer networks
- This section describes the characteristics and differences between TCP and UDP