MEMEPh. ideas that are worth sharing...

Comparison of TCP and UDP

Introduction


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

 

1. TCP/IP network model

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

TCP/IP is a general term for various protocol families related to the Internet, such as: TCP, UDP, IP, FTP, HTTP, ICMP, SMTP, etc., all belong to the protocols in the TCP/IP family.

The TCP/IP model is the foundation of the Internet and is an umbrella term for a series of network protocols. These protocols can be divided into four layers, namely link layer, network layer, transport layer and application layer.

In the network architecture, the establishment of network communication must be carried out at the peer layer of both sides of the communication, and cannot be interleaved. During the entire data transmission process, when the data passes through each layer at the sender, the protocol header and protocol tail of the corresponding layer must be attached (only the data link layer needs to encapsulate the protocol tail) part, that is, the data must be protocol encapsulated. to identify the communication protocol used by the corresponding layer. Next, there are two representative transport layer protocols in TCP/IP, TCP and UDP.

 

2. UDP

The full name of the UDP protocol is the User Datagram Protocol. It is used to process data packets in the same network as the TCP protocol. It is a connectionless protocol. In the OSI model, the fourth layer, the transport layer, is the upper layer of the IP protocol. UDP has the disadvantage that it does not provide packet grouping, assembly and sorting of packets, that is to say, when a packet is sent, it is impossible to know whether it arrives safely and completely.

It has the following features:

 

1. Towards the connectionless

First of all, UDP does not need to perform a three-way handshake to establish a connection like TCP before sending data. If you want to send data, you can start sending. And it is only a porter of data packets, and will not perform any splitting and splicing operations on data packets.

Specifically:

 

2. There are unicast, multicast and broadcast functions

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

 

3. UDP is message-oriented

The sender's UDP sends the message to the application, and then delivers it to the IP layer after adding the header. UDP does not merge or split the packets delivered by the application layer, but preserves the boundaries of these packets. Therefore, the application must choose the appropriate size of the message

 

4. Unreliability

First of all, the unreliability is reflected in the lack of connection. The communication does not need to establish a connection, and it can be sent as soon as it wants. This situation is definitely unreliable.

And what data is received will be transmitted, and the data will not be backed up, and the data will not be sent to the other party without caring whether the other party has received the data correctly.

In addition, the network environment is good and bad, but UDP will always send data at a constant speed because there is no congestion control. Even if the network conditions are 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 advantages are also obvious. In some scenarios with high real-time requirements (such as teleconferences), UDP instead of TCP needs to be used. As can be seen from the above dynamic diagram, UDP will only throw the data packets it wants to send to the other party, and does not care whether the data arrives safely and completely.

 

5. The header overhead is small, and it is very efficient to transmit data packets.

The UDP header contains the following data:

Therefore, the header overhead of UDP is small, only eight bytes, which is much less than at least twenty bytes of TCP, and it is very efficient when transmitting data packets.

 

3. TCP

When one computer wants to communicate with another computer, the communication between the two computers needs to be smooth and reliable, so as to ensure the correct sending and receiving of data. For example, when you want to view a web page or check an email, you want to see the web page in its entirety and in order without losing any content. When you download a file, you want to get the complete file, not just a part of the file, because if the data is lost or out of order, it's not what you want, so TCP is used.

The full name of the TCP protocol is Transmission Control Protocol, which 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, and you can think of it as the flow of water down a drain.

 

1. TCP connection process

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

 

first handshake

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

 

second handshake

After the server receives the connection request segment, if it agrees to the connection, it will send a response, and the response will also contain its own data communication initial sequence number. After the transmission is completed, it will enter the SYN-RECEIVED state.

 

third handshake

When the client receives the connection consent response, it also sends a confirmation message to the server. After the client sends this segment, it enters the ESTABLISHED state, and the server also enters the ESTABLISHED state after receiving the response. At this time, the connection is established successfully.

You may have a doubt here: Why does TCP need three handshakes to establish a connection instead of two? This is because this is to prevent an invalid connection request segment from being received by the server, resulting in an error.

 

2. TCP disconnect

TCP is full duplex, and both ends need to send FIN and ACK when disconnecting.

 

first handshake

If client A thinks the data transmission is complete, it needs to send a connection release request to server B.

 

second handshake

After B receives the connection release request, it will tell the application layer to release the TCP connection. Then it will send an ACK packet and enter the CLOSE_WAIT state, which indicates that the connection between A and B has been 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 B still has unfinished data at this time, it will continue to send, and after completion, it will send a connection release request to A, and then B will enter the LAST-ACK state.

 

fourth handshake

After receiving the release request, A sends a confirmation response to B, and A enters the TIME-WAIT state at this time. This state will last for 2MSL (maximum segment lifetime, which refers to the time that the segment survives in the network, and the timeout will be discarded). If there is no retransmission request from B within this time period, it will enter the CLOSED state. When B receives the acknowledgment, it also enters the CLOSED state.

 

3. Characteristics of TCP protocol

Connection-oriented means that a connection must be established at both ends before sending data. The method of establishing a connection is a "three-way handshake", which can establish a reliable connection. Establishing a connection lays the foundation for reliable data transmission.

 

Each TCP transmission connection can only have two endpoints, only point-to-point data transmission, and does not support multicast and broadcast transmission methods.

 

TCP does not transmit packets independently like UDP, but transmits them in a stream of bytes without preserving packet boundaries.

 

For reliable transmission, judging packet loss and bit error depends on the segment number and acknowledgment number of TCP. In order to ensure the reliability of message transmission, TCP gives each packet a sequence number, and the sequence number also ensures the orderly reception of packets transmitted to the receiving entity. The receiver entity then sends back a corresponding acknowledgment (ACK) for the successfully received bytes; if the sender entity does not receive an acknowledgment within a reasonable round-trip delay (RTT), then the corresponding data (assuming lost) will be retransmitted.

 

When the network is congested, TCP can reduce the rate and amount of data injected into the network and relieve the congestion

 

TCP allows applications on both sides of the communication to send data at any time, because both ends of the TCP connection have buffers to temporarily store data for bidirectional communication. Of course, TCP can send a segment immediately, or buffer it for a period of time to send more segments at once (maximum segment size depends on MSS)

 

4. Comparison of TCP and UDP

1. Contrast

  UDP TCP
whether to connect no connection connection oriented
Is it reliable Unreliable transmission, no flow control and congestion control are used Reliable transmission, using flow control and congestion control
Number of connection objects Support one-to-one, one-to-many, many-to-one and many-to-many interactive communication only one-to-one communication
transfer method message oriented byte stream
header overhead The header overhead is small, only 8 bytes Header minimum 20 bytes, maximum 60 bytes
Applicable scene Ideal for real-time applications (IP telephony, video conferencing, live streaming, etc.) Ideal for applications that require reliable transfers, such as file transfers

 

2. Summary

TCP provides connection-oriented reliable services to upper layers, and UDP provides connectionless unreliable services to upper layers.

Although UDP is not as accurate as TCP transmission, it can also make a difference in many places with high real-time requirements

For high data accuracy requirements, the speed can be relatively slow, you can choose TCP