Summary of TCP and UDP

Source: Internet
Author: User

1. What are TCP and UDP?
TCP and UDP are two transport layer protocols in the TCP/IP protocol. They use the IP routing function to send data packets to the destination for the ApplicationProgramAnd the application layer protocol (including HTTP, SMTP, SNMP, FTP, and telnet) to provide network services. TCP provides connection-oriented and reliable data stream transmission, while UDP provides non-connection-oriented and unreliable data stream transmission. The connection-oriented protocol establishes point-to-point connections before any data transmission. ATM and Frame Relay are connection-oriented protocols, but they work on the data link layer rather than on the transport layer. Common audio phones are also connected.
Reliable transmission protocols can avoid data transmission errors. The implementation method is: when constructing a data packet, set the verification code in it, and then use a certain numberAlgorithmRe-calculate the verification code. By comparing the two, you can find the damaged data. To resend corrupted data and lost data, the protocol must enable the destination to provide a confirmation signal from the source when re-sending data. Some packets do not necessarily arrive in order, so the Protocol must be able to detect unordered packets, store them temporarily, and then send them to the application layer in the correct order. In addition, the Protocol must be able to identify and discard repeatedly sent data. A set of timers can limit the wait time for different confirmations, so that you can start resending or re-establish a connection.
The data stream transmission protocol does not support bit transmission. TCP cannot construct data in bytes or bits in a package. It is only responsible for transmitting unconstructed 8-bit strings.
The non-connection-oriented transmission protocol does not establish a connection before data transmission, but routes non-connection-oriented packets and packets at each intermediate node. No point-to-point connections, non-connection-oriented protocols, such as UDP, are unreliable connections. When a UDP packet moves in the network, the sending process does not know whether it has reached the destination, unless the application layer has confirmed the fact that it has arrived. Non-connection-oriented protocols cannot detect duplicate and out-of-order packets. Standard Terminology uses "unreliable" to describe UDP. In modern networks, UDP is not easy to cause transmission failures, but you cannot say it is reliable.

Ii. TCP Workflow
Now let's take a look at the various fields of the TCP segment. In the IP packet, they follow the IP header information. The first 16-bit confirms the source port, and the second 16-bit confirms the destination port. The division of ports enables different types of concurrent connections between IP hosts with a single IP address. In most modern operating systems, 32-bit IP addresses and 16-bit port addresses are used to confirm an interface. The combination of the source interface and the target interface defines a connection. There are 216 or 65536 possible ports. The lowest 1024 ports are commonly used, which are the default settings reserved by the system for specific application layer protocols. For example, HTTP uses port 80 by default, while POP3 uses port 110. Other applications can use ports with higher numbers.
In the next two domains, the serial number and validation number are the key to TCP's reliable connection. When a TCP connection is established, the sender host sends a random initialization serial number to the initialization device. The initialization device adds it to 1 and sends it back to the start device of the validation domain, this means that the next byte can be sent. Once the data begins to flow, the serial number and validation number track the data that has been sent and that data has been confirmed. Because each domain is 32 bits, a total of 232 values are allowed, so the range of each domain is: 0 ~ 4294967295. Return to 0 when the upper limit is exceeded.
The offset of four bits indicates the total number of 32 bits in the TCP header. This information is indispensable, because there is an optional Header area, the offset identifies the end Of the header and the start of the data.
The designers of TCP keep the next six digits, just in case they will be extended in the future. In fact, since the release of rfc793 (Transport Control Protocol) in 1981, no one has had the right reason to use these bits. At this point, Jon Postel and his colleagues must be overly cautious.
Each of the next six digits is a sign. If the value of the UNG flag is 1, it means that the data in the emergency pointer area of the header is valid. If the value of the ACK flag is 1, this means that the data in the "Confirm" area is valid. (Note: a syn Packet has a meaningful serial number, but its validation number is meaningless because it does not confirm any events) the PSH flag makes it unnecessary to wait for the data to be sent or received. The RST flag is disconnected. The Syn (synchronous) flag indicates that the serial number is valid, and the fin (final) flag indicates that the sender has sent the data.
The 16-bit long window area shows the size of the sliding window, that is, to tell the sender how many words it has prepared to receive data. TCP controls data traffic by adjusting the window size. A window with a value of 0 means to notify the sender: if no further notification is sent, the receiver is full and cannot receive more data. A large window ensures that up to 65536 unconfirmed bytes are transmitted at any given time. However, when the resend timer times out and is not confirmed, the window is halved, this effectively reduces the transmission rate.
The 16-bit verification code area ensures data integrity and protects all Regions of the TCP header and IP header. The sender calculates the verification value and inserts it into this region. The receiver recalculates the value based on the received package and compares the two. If they match, the data is considered intact.
When an emergency flag is set, the emergency pointer is a 16-bit offset, which represents the last word that must be accelerated. The selected area can contain 0 or more 32-bit characters, which can expand the performance of TCP. Most commonly used selection regions support a window larger than 65536 bytes, which shortens the waiting time for confirmation, especially at high transmission rates.
The transmission mechanism of TCP has multiple timers. When a packet is sent, the resend timer starts counting. When a confirmation signal is received, the resend timer stops counting. If no confirmation signal is received after the specified time period, resend the package. A tricky issue is how to set the time period. If it is too long, increasing network transmission errors will lead to unnecessary waiting time. If it is too short, too many duplicate packets will be generated, thus reducing the network response time. The modern TCP protocol dynamically sets the re-timer according to the actual situation.
The continuous timer is essential to avoid deadlocks. If the network receives a confirmation window with a size of 0 and the subsequent resend data is lost, the timer will time out and send a probe. The probe response will indicate the window size (maybe still 0 ). After the timer is not active at the local end, the system checks whether there are any running processes on the other end of the connection. If no response is received, the timer will be disconnected.
When a connection is disconnected, the disconnect timer doubles the maximum lifetime of the package. This timer ensures the maximum traffic before the connection is disconnected.
No matter how effective the re-sending process is, few dropped packets can seriously reduce the traffic of TCP connections. Each unreceived package or package segment will be lost only when the resend timer times out. During data re-transmission, the receiving process continues to deliver the re-transmission data, causing a pause in the overall data transmission until the lost data is replaced. These re-transmission processes cause TCP-based connections to be sometimes unstable.

Iii. Selection of TCP and UDP
If we compare the structure of UDP packets and TCP packets, it is obvious that UDP packets do not have the complex reliability and Control Mechanism of TCP packets. Similar to TCP, UDP supports multiple applications on one host as well as the number of source ports and destination ports. A 16-bit UDP packet contains a Byte Length header and Data Length. The verification code field enables it to perform overall verification. (Many applications only support udp, such as multimedia data streams, and do not generate any additional data. Even if you know the damaged packets, do not resend them .)
Obviously, when the performance of data transmission must be in the integrity, controllability, and reliability of data transmission, TCP is certainly the choice. UDP is the best choice for audio and multimedia applications that emphasize transmission performance rather than transmission integrity. When the data transmission time is so short that the previous connection process becomes the whole traffic subject, UDP is also a good choice, such as DNS switching. The reason for establishing SNMP on UDP is that the designer believes that when the network is blocked, the low overhead of UDP gives it a better chance to transmit and manage data. The rich functions of TCP sometimes lead to unpredictable low performance, but we believe that in the near future, reliable point-to-point connections of TCP will be used in most network applications.

Iv. Summary

The two are based on the difference between connection and no connection. As a result, TCP ensures data correctness and UDP may cause packet loss. TCP ensures data sequence, but UDP does not. TCP has more requirements on system resources than UDP.
Difference Between TCP and UDP:
1. Connection-based and connectionless
2. Requirements on system resources (more TCP and less UDP)
3. simple UDP program structure
4. stream mode and datagram Mode
5. TCP ensures data correctness, UDP may cause packet loss, TCP ensures data order, and UDP does not guarantee
Industrial Applications generally have the following features:
1. It is required to transmit data from time to time, but there are also some cases of regular transmission. In general, the server center and GPRS terminal devices are required to transmit data from time to time throughout the transmission process.
TCP itself is a reliable link transmission, providing a two-way transmission channel from time to time, which can meet the requirements of industrial field transmission. However, the GPRS network also has a limit on the TCP link: this link has no data traffic for a long time (about 20 minutes, depending on the actual situation, the priority of this link is automatically lowered until the link is forcibly disconnected. Therefore, the heartbeat packet (usually one byte of data) is used to maintain the link.
Due to its own characteristics and the limited resources of the UDP port in the GPRS network, UDP is easy to change when there is no data traffic for a period of time. The impact is to send data from the server center to the GPRS terminal, the GPRS terminal cannot receive the message. The specific reason is that the mobile gateway has transitioned from the port, and a UDP packet needs to be sent to the host every certain time to maintain the IP address and port number, in this way, the host can actively send UDP packets to GPRS and I found in the test that the interval is very short. I can only maintain the UDP packet once in more than one minute, however, the mobile Gateway may lose this port for a long time. If the host wants to send data to GPRS, it will definitely not work. Only the GPRS terminal device sends another UDP packet, move and assign you a transit IP address and port for two-way communication.
2. The packet loss rate is small. In some industrial scenarios, such as electric power, water meter reading, and environmental monitoring, data loss during transmission is not allowed or data reliability is required to the maximum extent.
From this point of view, it is clear that in the wireless data transmission process, TCP is better than UDP to ensure data integrity and reliability, and there is a smaller packet loss rate. This is also true in actual tests. Taking the GPRS terminal equipment provided by Xiamen lansi communication Co., Ltd. as an example: TCP is deployed at 9 in and UDP is deployed at around 17 in.
3. reduce the cost. At present, many GPRS devices are used to replace early-stage radio data transmission stations. In addition to the scope of use, the main consideration is cost. The cost reduction is of course the most acceptable. Directly related to the cost is the traffic, low traffic, and low cost.
Although TCP has more headers than UDP, UDP usually needs to maintain two-way channels in actual applications, so it must maintain port resources through a large amount of Heartbeat packet data. In general, the actual traffic of UDP is larger than that of TCP. Many users do not know that UDP requires a large number of Heartbeat packets to maintain port resources in the early stage. Generally, UDP is considered to be more traffic-saving than TCP. In fact, there is a misunderstanding.
4. In some specific application scenarios, such as the time-to-time interaction systems of some banks, the response speed is very high. At this time, the data transmission frequency is fast, and a large number of Heartbeat packets are not required to maintain UDP port resources, using UDP is advantageous.
5. In the current 1: N transmission mode, there are multiple GPRS terminal devices that transmit data to a server center. UDP is much better than TCP at this time, because UDP consumes less system resources. However, in practical applications, it is found that many users still use the TCP transmission mode to establish a second-level Center 1: A (1: N), that is, each sub-center corresponds to N/A devices, data is processed independently and then transmitted to the master center in a unified manner. This not only ensures that the TCP transmission protocol is used in the transmission process, but also well handles the problem of system consumption of multiple links of the central server.

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.