TCP vs UDP – Difference and Comparison
TCP vs UDP – Difference and Comparison
TCP / IP five-layer network structure model
- Physical layer: The physical layer is based on the physical communication medium, as the system and communication medium interface, used to achieve data link entities transparent bit (bit) streaming. Only the layer for the real physical communication, the other layers for the virtual communication
- Data link layer: In the physical layer to provide bit stream service on the basis of the establishment of the data link between adjacent nodes, through the error control to provide data frames (Frame) on the channel error-free transmission, and the circuit Of the action series. The unit of data is called a frame.
- Network layer: Select the appropriate route so that the data packet can be delivered to the destination host
- Transport layer: responsible for communication between hosts in the process
- Application layer: directly for the user’s application services
Transport layer
We know that the transport layer is in the process of transmission of messages, while TCP protocol, UDP protocol is TCP / IP in the most representative of the transport layer protocol. The following summarizes the similarities and differences between the two protocols and how the transport layer works.
TCP and UDP distinction:
TCP protocol: connection-oriented, reliable flow protocol. Connection refers to the two applications in order to pass information to each other proprietary, virtual communication lines, also known as virtual circuits. Flow refers to the uninterrupted data structure, similar to the flow of water in the pipeline. Reliability refers to the TCP protocol to provide reliable transmission, the implementation of “sequential control” or “retransmission control” mechanism. In addition, there are “flow control”, “congestion control” to provide network utilization and many other features.
UDP protocol: a datagram protocol that does not have a reliability. Only to ensure that the message, the other processing by the upper application to complete.
Wow! TCP so many features, is not necessarily worse than UDP? In fact, they have their own application scenarios. TCP application scenarios: efficiency requirements are relatively low, but the relatively high accuracy requirements of the scene. Because the transmission needs to confirm the data, retransmission, sorting and other operations, compared to the efficiency of no high UDP. Give a few examples: file transfer (accurate high demand, but the speed can be relatively slow), accept mail, remote login.
UDP application scenarios: the efficiency requirements are relatively high, the relatively low accuracy requirements of the scene. Give a few examples: QQ chat, online video, network voice calls (instant messaging, speed requirements are high, but occasional intermittent is not too much problem, and here can not use the retransmission mechanism), broadcast communication broadcast).
Transmission communication:
Two protocols are inter-process communication, that is, between the application of communication, then how many programs to find their own purpose applications? At the transport layer, use the port number to identify different applications that communicate in the same computer.
In general, it is possible to identify a communication based on the source IP address, the destination IP address, the source port number, and the destination port number. However, there are some special cases, such as IP addresses and port numbers. The transmission agreement is not the same, how to distinguish? After the data arrives at the IP layer (network layer), it checks the protocol number of the IP header and then passes it to the module of the corresponding protocol.
Therefore, TCP / IP or UDP / IP communication usually uses five messages to identify a communication: “source IP address”, “destination IP address”, “source port number”, “destination port number”, and “protocol number”. (Well-known port number and transport layer protocol does not matter, for example, 53 ports in TCP, UDP are used for DNS services)
Port number is determined: the standard established port number, 0-1023 for the well-known port number, the other has been officially registered port number is 1024-49151; dynamic allocation of port numbers, the operating system to assign the application does not conflict with the port number, The next port number is incremented by 1 on the previous allocation number and dynamically assigned the port number range 49152-65535.
UDP detailed solution
UDP is the User Datagram Protocol abbreviation. UDP does not provide complex control mechanisms, the use of IP to provide connectionless communication services. And it is the application of the data sent at the moment, immediately as it was sent to the network as a mechanism.
Why does UDP exist? What are the advantages?
- No connection required (reduced delay)
- Simple implementation: no maintenance connection status required
- Head overhead is small
- No congestion control: the application can better control the sending time and send rate
UDP header:
The UDP header consists of the source port number, the destination port number, the packet length, and the checksum. Checksum is mainly used to detect whether the UDP segment in the transmission of an error occurred. There is also that the checksum calculation also needs to calculate UDP pseudo-head, the pseudo-header contains some fields of the IP header. We have just introduced five messages to identify a communication, and UDP header only port number, the remaining three in the IP header, so the introduction of the concept of pseudo-head. (IPv6 IP header does not have checksum field)
There are some scenarios need to take into account the reliability and efficiency, then how to achieve reliable data transmission on UDP?
TCP Detailed
TCP through the checksum, serial number, confirm the response, retransmission control, connection management and window control mechanisms such as reliability transmission.
Connection management:
Data communication must be done before the connection work, TCP connection in the establishment of the need for three times the handshake, while the end of the communication will be disconnected processing (four waving). A connection to establish and disconnect, the normal process at least need to send back and forth 7 packets to complete.
In TCP, when the sender’s data arrives at the host, the receiving host returns a notification of the received message, which is called ACK (Positive Acknowledgment). If no ACK is received, then it is likely that a packet loss or return confirmation is lost on the way. In addition, it may be due to other reasons that the ACK delay arrives. If the sender does not receive an ACK, it will retransmit, but there will be duplicate send and receive for delay and ACK loss. So we introduced a mechanism to identify whether the data has been received, but also to identify whether it has been received.
The above repetitive control functions can be implemented by serial numbers. The serial number is in the order of each byte to send data are marked with the number of the number, the receiver to receive data in the TCP header serial number and the length of the data, the next step should be received by the serial number as a confirmation number back to return The Through the serial number and confirm the response number, TCP to achieve reliable transmission.
Increase the speed with window control
If we make a confirmation every time we send a segment, the longer the round trip, the worse the throughput will be and the lower the communication performance will be.
In order to solve this problem, TCP introduces the concept of the window. The acknowledgment is no longer confirmed by each segment, but with a larger unit (window size), and the forwarding time is significantly reduced. As for the size of the window is determined by the receiver host, but also facilitate the flow control.
Window control and retransmission control
Allows the sender to send multiple packets continuously before the ACK is received. For the case where the segment is lost, we discuss the window control.
For the previous delay ACK, after using the window control, you can receive the acknowledgment before sending the message, so that the overall speed is greatly improved.
For the case where the confirmation response failed to be returned. When the window control is not used, the data that does not receive the acknowledgment response will be retransmitted, and the window control is used. Some acknowledgments are lost and need not be retransmitted. Can be confirmed according to their own confirmation or the next confirmation to confirm.
For the loss of the message segment. When a packet is lost, the sending end will receive multiple acknowledgment responses of serial number 1001 to remind the sending end to send the message again. For the sender, if the same acknowledgment response is received three times in succession, the corresponding data will be retransmitted.