Detailed description and comparison of transport layer protocol TCP and UDP

Source: Internet
Author: User
Tags ftp protocol

TCP/IP is a commonly used standard protocol for network interconnection. It can communicate with each other in different environments and nodes, it is the protocol used by all computers connected to the Internet to exchange and transmit various information over the network. It is also the protocol used for interconnection between Windows NT, Windows 2000 Server, NetWare and UNIX. TCP/IP is actually a hierarchical protocol. It contains many other protocols and forms a TCP/IP protocol group. The Protocol hierarchies are as follows:

 

Table 1 TCP/IP protocol layers

 

Application Layer

HTTP, Telnet, FTP, SMTP, SNMP

Transmission Layer

TCP and UDP

Inter-network layer

IP (ARP, RARP, ICMP)

Network Interface Layer

Ethernet, X.25, SLIP, PPP

 

TCP/IP is actually a complete set of network protocols on the physical network. TCP provides transport layer services, while IP provides network layer services. The core part of TCP/IP is the transport layer protocol (TCP, UDP), network layer protocol (IP), and physical interface layer, which are usually implemented in the operating system kernel.

Figure 1 TCP/IP protocols

The following describes the Differences Between TCP and UDP in the transport layer.

1. User Datagram Protocol (UDP)

User Data Protocol (UDP) is a connectionless transport layer Protocol in the OSI reference model. It provides a simple and unreliable information transmission service for transactions; the UDP protocol is basically an interface between the IP protocol and the upper-layer protocol. The applicable port of the UDP protocol runs multiple applications on the same device. It is a non-connection-oriented protocol, it does not establish a connection with the other party, but directly sends data packets. UDP is applicable to applications that transmit only a small amount of data at a time and do not require high reliability. For example, we often use the ping command to test whether the TCP/IP communication between two hosts is normal. The actual ping command is to send UDP packets to the other host, then, the host of the other party confirms that the packet is received. If the message sent when the packet arrives is returned in time, the network is connected.

The source port and target port fields of UDP packets specify two port numbers with a length of 16. The source port field is optional. If the value of this field is specified, indicates the port number to which the corresponding data packet should be sent. If it is not used, set it to 0. The length field indicates the 8-bit characters of the entire UDP packet, including the UDP header and data section. Therefore, the minimum value of this field is 8. Description:

Figure 2 UDP datagram format

UDP is located above the IP protocol, that is, UDP packets are encapsulated in IP packets for transmission.

UDP Header

UDP data Zone

 

IP Header

IP packet data Zone

 

Frame Header

Frame data Zone

Figure 3 UDP Packet encapsulation

The following explains how to transmit data to A specific application of the target host A when a udp-based application is used. First, the application that receives the data needs to apply for a UDP port number and set it to P. After the sender's application prepares the data, it submits it to the UDP protocol and sends the data to port P of host. UDP encapsulates the application data as the Data Part Of The UDP data packet in a UDP data packet, and sets the destination port field of the data packet to P. The UDP protocol then sends the packet to the IP protocol for processing and sends the packet to host. The IP protocol encapsulates UDP data packets as IP data packets in an IP data packet, sets the destination address to A, and sets the protocol field to 17, then it is sent to the network layer for processing. The IP packet may pass through several routers and eventually reach the IP protocol layer of host. When the IP protocol discovery field of host A is 17, the data zone of the IP data packet is handed over to the UDP protocol for processing. If the UDP protocol finds that the port number is P, the data zone of the UDP data packet is placed in the queue of port P. A's application extracts data from the queue for processing.

Ii. Transmission Control Protocol TCP

Transport Control Protocol TCP (Transmission Control Protocol) is a connection-based Protocol, that is, a connection must be established with the other party before formal communication. A tcp connection can be established only after three "handshakes". The three simple processes include: host A sends connection request packets to host B; host B sends A data packet to host A that agrees to the connection and requires synchronization (synchronization means that two hosts are sending, receiving, and coordinating; host A then sends A packet to confirm that host B requires synchronization. The purpose of the three "dialogs" is to synchronize the transmission and receipt of data packets. After the three "dialogs", host A formally sends data to host B.

The services provided by TCP to applications are oriented to byte streams. The transmission of TCP data is implemented through the IP protocol. The transmission unit of the IP protocol is IP packets. Because the byte stream data provided by the user may be large, and the data that an IP packet can accommodate is limited, therefore, the TCP protocol must split the byte stream data and organize it into IP data packets for transmission. The TCP protocol on the target host then organizes the split data into data streams. A tcp packet has its own header and packet, and a TCP packet becomes a segment. The source port and target port are used to specify the TCP port number of the sender and receiver. Different from UDP, the source port number in the TCP segment must be specified. This is because TCP is connection-oriented. a TCP connection consists of the IP address of the sender and receiver and the TCP port number. Because a TCP port number can be used for different links, the absence of one of the two ports cannot determine the TCP link to which the data segment belongs, and the application for data processing cannot be determined. The specific format of the TCP data segment:

 

Figure 4 TCP Message format

The establishment of a TCP connection requires sending/receiving of three connection information. The purpose of the three-way handshake is to synchronize the initial serial number of the data sent by both parties. First, the connection active open method (that is, the connection requester) sends a TCP data segment to the passive open party (that is, the connection receiver), which usually does not contain the data zone, set SYN position 1 in the code bit to an initial serial number in the serial number field. After the passive open party receives the link request segment, it sends a TCP segment to the active open party. Set ACK to 1, indicating that the connection request from the active open party has been received. If you enable it passively, the system sets the SYN position 1 and its initial serial number in the serial number field. After the active open party receives the TCP segment of the passive open party, it sends an ACK to the passive open party. After the receiver receives the TCP segment, the connection is established and data transmission starts.

Figure 5 three-way handshake process for TCP connection Establishment

Iii. Comparison of TCP and UDP

Because the UDP protocol is not connected, its communication efficiency is high. Because of this, its reliability is not as high as that of the TCP protocol.

TCP provides reliable transmission services, while UDP provides unreliable services. UDP does not have a traffic control mechanism. If the sending process sends a datagram filled with the receiving buffer of the receiving process, the datagram is discarded, UDP does not notify the sending process to slow down the data transmission rate. The TCP protocol has traffic control, and the specific mechanism is omitted.

 

TCP provides byte stream-oriented services. The application only needs to submit the data to be transmitted to the TCP protocol in the form of a byte stream. In another segment of the connection, the data appears in the receiving program in the same byte stream order. The transmission unit of UDP protocol is data block. One data block can only be encapsulated in one UDP data packet.

TCP is not applicable to the case where only a small amount of data is transmitted. Because the connection overhead is large, this is not worth the candle. In addition, TCP is not applicable to real-time applications, because the TCP protocol transmits data sequentially, only the previous transmission is successful before subsequent data transmission starts. This obviously does not meet the requirements of real-time applications.

For applications that are not suitable for using the TCP protocol, only UDP can be used. When using the UDP protocol for communication, the application must handle the following problems by itself: (1) the application must provide its own mechanism to ensure reliability. Applications must have their own time-out and re-transmission mechanisms, data out-of-order processing, and traffic control. Of course, you do not need these mechanisms for applications with low reliability requirements, but usually you need to differentiate the Data Order. (2) Applications must process the segmentation of large data blocks so that they can be encapsulated in a UDP packet. The receiver must reorganize the split data.

In UDP, when sending each datagram, the sending process does not wait for multiple datagram to be centralized and sent together as a large datagram, but immediately sends it out. It is a record-type protocol. In addition, the datagram obtained by the receiving process through read or recv each time must be the datagram sent by the sending process. It cannot be multiple datagram, the receiving process can identify the record boundary of each datagram sent by the sending process. In TCP, the sending process may not send each datagram immediately during kernel processing, instead, multiple data packets are sent together with a large data packet, which is a byte stream protocol. The receiving process reads the datagram sent by the sending process through read each time. It does not necessarily mean that the sending process originally sent the datagram. The receiving process cannot identify the record boundary of each datagram, therefore, the TCP protocol is a byte stream protocol with no record boundaries. For example, the protocol used for QQ chat should have a record boundary. In the chat process, the unit is "message", and the message can be regarded as a record. Therefore, the QQ chat protocol uses UDP rather than TCP protocol.

UDP is an unordered transmission protocol. Each datagram sent by the sending process does not arrive at the receiving process according to the original sending order. It is possible that the datagram sent earlier may arrive at the receiving process later, when a datagram is transmitted through an intermediate path, different data transmission paths or other reasons may lead to different data transmission sequences. In order to make UDP-based applications orderly, sequence numbers and validation mechanisms must be set in the application. The TFTP protocol is a UDP-based protocol, and the TCP protocol is an ordered protocol with timeout, serial number, retransmission, and validation mechanisms. For example, the FTP protocol is based on the TCP protocol for transferring files, and the control connection in the FTP protocol transmits content in the message form. The client sends a request message on the control connection, the server returns a request result message. Because the control connection is interactive message transmission, the client sends a request before the server responds to the message, the client will not send the second request message, so the TCP protocol is used for interactive message transmission, so we do not have to worry about the overlapping of the two request messages.

After creating a plug-in, UDP can establish communication with multiple servers. However, TCP can only communicate with one server. TCP does not allow the target address to be a broadcast or multicast address, UDP allows. The communication relationship between UDP client and server can be one-to-many, while TCP can only be one-to-one.

Table 2: Client connection process

Table 3: server connection process

 

From the Garden of Eden

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.