TCP and UDP comparison

  Front end, java, javascript, node.js, Programmer


Network protocol is the knowledge that every front-end engineer must master. TCP/IP has two representative transport layer protocols, TCP and UDP respectively. This article will introduce the two and their differences.

If you want to read more excellent articles, please stamp them fiercely.GitHub blog

I. TCP/IP network model

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

TCP/IP is the general name of various protocol families related to the Internet, such as TCP, UDP, IP, FTP, HTTP, ICMP, SMTP, etc. All belong to the protocols within the TCP/IP family.

TCP/IP model is the foundation of the Internet and 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: responsible for encapsulating and unpacking IP messages, sending and receiving ARP/RARP messages, etc.
  • Network layer: responsible for routing and sending packet messages to the target network or host.
  • Transport layer: responsible for grouping and reassembling the messages and encapsulating the messages in TCP or UDP protocol format.
  • Application layer: responsible for providing applications to users, such as HTTP, FTP, Telnet, DNS, SMTP, etc.

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


The full name of UDP protocol is user datagram protocol, which is used to process data packets in the network just like TCP protocol, and is a connectionless protocol. In the OSI model, the fourth layer, the transport layer, is one layer above the IP protocol. UDP has the disadvantages of not providing packet grouping, assembling and not being able to sort packets, that is to say, when a message is sent, it is impossible to know whether it arrives safely and completely.

It has the following characteristics:

1. For connectionless

First of all, UDP does not need to establish a connection with three-way handshake before sending data, just like TCP. You can start sending data if you want. Moreover, it is only a porter of data messages and will not carry out any splitting and splicing operations on data messages.


  • At the sending end, the application layer transfers the data to the UDP protocol of the transport layer. UDP will only add a UDP header to the data, which is the UDP protocol, and then it will be transferred to the network layer
  • At the receiving end, the network layer transfers the data to the transport layer, UDP only removes the IP packet header and transfers it to the application layer without any splicing operation

2. 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, which means UDP provides unicast, multicast and broadcast functions.

3.UDP is message oriented

UDP of the sender delivers the message delivered by the application program to the IP layer after adding the header. UDP does not merge or split the messages delivered by the application layer, but keeps the boundaries of these messages. Therefore, the application program must select a message of appropriate size

4. unreliability

First of all, the unreliability is reflected in the lack of connection. Communications do not need to establish a connection, and they can be sent whenever they want. Such a situation is certainly not reliable.

And will not back up the data or care whether the other party has received the data correctly.

In addition, the network environment has ups and downs, but UDP always sends data at a constant speed because there is no congestion control. Even if the network conditions are not good, the transmission rate will not be adjusted. The disadvantage of this implementation is that packet loss may occur under poor network conditions, but the advantage is also obvious. UDP is needed instead of TCP in some scenes with high real-time requirements (such as teleconferencing).

From the above dynamic diagram, we can know that UDP will only throw the data messages it wants to send to the other party, regardless of whether the data arrive safely and completely.

5. The overhead of the header is small, and it is very efficient when transmitting data messages.

The UDP header contains the following data:

  • Two 16-bit port numbers, the source port (optional field) and the destination port
  • The length of the entire data message
  • Check sum of the entire data message (IPv4 optional field), which is used to find errors in header information and data

Therefore, UDP has a small overhead of eight bytes, much less than TCP’s at least 20 bytes, and is very efficient in transmitting data messages.


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 email, you want to view the web page completely and sequentially without losing anything. When you download a file, what you want is the complete file, not just a part of the file, because if the data is lost or out of order, it is not the result you want, so TCP is used.

The full name of TCP is Transmission Control Protocol, which is a connection-oriented, reliable, byte stream-based transport layer communication protocol defined by IETF RFC 793. TCP is a connection-oriented and reliable flow protocol. Flow is an uninterrupted data structure, which you can think of as the flow of water in a drain.

1.TCP connection process

As shown in the following figure, it can be seen that the process of establishing a TCP connection is (the process of three-way handshake):

First handshake

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

Second handshake

After receiving the connection request message segment, if the server agrees to connect, it will send a response, which will also include its own initial serial number of data communication. After sending, it will enter SYN-RECEIVED status.

First three-way handshake

When the client receives the response of connection approval, it will also send a confirmation message to the server. The client enters the ESTABLISHED state after sending this message segment, and the server also enters the ESTABLISHED state after receiving this response. At this time, the connection is established successfully.

There may be a question: Why does TCP need three-way handshake to establish a connection instead of twice? This is because this is to prevent the failure of the connection request message segment received by the server, resulting in errors.

2.TCP Disconnect Link

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

First handshake

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

Second handshake

After receiving the connection release request, b will tell the application layer to release the TCP link. 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 TCP connections are bidirectional, B can still send data to A.

First three-way handshake

B if there is still unfinished data at this time, it will send a connection release request to a, and then b will enter the LAST-ACK state.

The fourth handshake

After receiving the release request, a sends a confirmation reply to b, at which time a enters the TIME-WAIT state. This state will last for 2MSL (maximum segment lifetime, which refers to the lifetime of the message segment 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 acknowledgement, it also enters the CLOSED state.

3. Characteristics of 3.TCP Protocol

  • Connection-oriented

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

  • Unicast transmission only supported

Each TCP transmission connection can only have two endpoints and can only carry out point-to-point data transmission. Multicast and broadcast transmission methods are not supported.

  • Byte-oriented stream

TCP does not transmit messages independently like UDP, but transmits in byte stream mode without reserving message boundaries.

  • Reliable transmission

    For reliable transmission, packet loss is judged by TCP segment number and acknowledgement number. In order to ensure the reliability of message transmission, TCP assigns a serial number to each packet, and the serial number also ensures the sequential reception of packets transmitted to the receiving entity. Then the receiving entity sends back a co rresponding acknowledgement (ACK) to the byte that has been successfully received; If the sending entity does not receive an acknowledgement within a reasonable round trip time delay (RTT), the corresponding data (assuming 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 alleviate congestion.

  • TCP provides full duplex communication

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

Iv. comparison between TCP and UDP

1. Comparison

Are you connected No connection Connection-oriented
Is it reliable Unreliable transmission, no 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 interactive communication It can only be one-to-one communication.
transmission mode Message oriented Byte-oriented stream
Head overhead Head overhead is small, only 8 bytes The header is 20 bytes minimum and 60 bytes maximum.
Applicable scenario Suitable for real-time applications (IP phone, video conference, live broadcast, etc.) It is suitable for applications requiring reliable transmission, such as file transmission.

2. Summary

  • TCP provides reliable connection-oriented 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 with high real-time requirements.
  • For those with high requirements on data accuracy and relatively slow speed, TCP can be selected.

To recommend a useful BUG monitoring toolFundebug, welcome to try free!

Welcome to pay attention to the public number:Front end craftsmanWe witness your growth together! If you feel fruitful, please give me a reward to encourage me to output more high-quality open source content.

Reference articles and books