Difference between UDP and TCP

Source: Internet
Author: User

The difference between packet-oriented (UDP) and byte stream (TCP) is that the packet transmission mode is the maximum number of packets sent to UDP at the application layer, and UDP sends one packet at a time. Therefore, the application must select the appropriate size of packets. If the message is too long, the IP layer needs to be split to reduce the efficiency. If it is too short, the IP address is too small. UDP does not merge or split packets handed over to the application layer, but retains the boundaries of these packets. That is to say, when the application layer sends the UDP packet, UDP sends the packet at a time. For byte streams, although the interaction between the application and TCP is a data block (varying in size), TCP regards the application as a series of unstructured byte streams. TCP has a buffer. When the data block transmitted by the application is too long, TCP can divide it into shorter ones and then transmit it. If the application sends only one byte at a time, TCP can wait for the accumulated bytes to form a packet segment for sending. It is an application of TCP and UDP protocols. Is a comparison of TCP and UDP protocols. Here we will detail the differences between connection-oriented and connection-free: connection-oriented example: communication between two people by phone; connection-oriented example: postal service, the user puts the mail in the mail and expects the postal processing process to deliver the postal package. Obviously, "not reachable" means "unreliable. From the perspective of program implementation, it can be used for description. It can be clearly seen that TCP communication requires the server to listen, receive client connection request accept, and wait until the client connect establishes a connection before sending and receiving data packets (recv/send. The concept of UDP is not obvious between the server and the client. The server, that is, the acceptor, needs to bind a port to wait for the arrival of client data. Later, you can send and receive data (recvfrom/sendto. As mentioned above, UDP retains the boundary of packets. Let's talk about the boundary between TCP and UDP. In the default blocking mode, TCP has no boundary and UDP has boundary. For the TCP protocol, the client sends data continuously. As long as the buffer of this function on the server side is large enough, it will receive the data at one time. That is, the client sends the data several times and has boundaries, the server side receives the data at a time, which proves to be borderless. For UDP protocol, the client continuously sends data, even if the buffer of this function on the server side is large enough, it can also be received only once and how many times it is sent, that is, when the client sends it several times, the server must receive it several times to prove that, this UDP communication mode has boundaries. TCP does not have a boundary, causing trouble in receiving data sent using the TCP protocol. It is easy to stick packets when receiving data, that is, the packet data sent by the sender is pasted into a package when it is received by the receiver. Because TCP is a stream protocol, for a socket packet, such as sending 10 aaaaabbbbb twice, the packet is divided into two for the first time due to network reasons, 10 aaaaaaab and BBBB, if you read 10 (package length) before reading the subsequent data when receiving a packet, when receiving fast and sending slowly, 10 AAAAAB is received first, which will explain an error, after receiving the BBBB10AAAAABBBBB message, it also explains the error. This is the TCP stick package. In network transmission applications, you usually need to customize a protocol encapsulation over the network protocol. A simple approach is to add a custom header before the data to be sent, the header can contain the data length and other information. When receiving the packet, the header is first received, and then the subsequent data is received Based on the Data Length described in the header. The detailed method is to first receive the packet header and specify the length of the packet body in the packet header to receive the packet. Set the checkpoint at the end of the packet header (for example, it starts with 0xAA and ends with 0xCC to check whether a packet is complete ). For TCP: 1) There is no packet loss or an error packet, so no data error occurs; 2) if the packet header is detected incorrectly, that is, it is illegal or requested, You can reset it directly. To avoid sticking packets, you can take the following measures. 1. For the packet sticking caused by the sender, you can use programming settings to avoid it. TCP provides an operation command push to force data to be transmitted immediately. After the TCP software receives the operation command, this section of data is immediately sent out, without waiting for the sending buffer to be full; 2. For the sticky packet caused by the receiver, you can optimize the program design, streamline the workload of the receiving process, and increase the priority of the receiving process so that it can receive data in a timely manner, so as to avoid sticking packets. 3. Controlled by the receiver, A packet of data is manually controlled and received multiple times based on the structure field, and then merged to avoid sticking to the packet.

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.