The transmission process of TCP and UDP and the difference between them

Source: Internet
Author: User
Difference Between TCP and UDP
TCP
The TCP/IP protocol is a protocol cluster. It includes many protocols. UDP is only one of them. The reason for naming is TCP/IP, because TCP and ipprotocol are two important protocols, they are used for naming. The TCP and UDP protocols belong to the transport layer protocol, while the IP protocol belongs to the network layer protocol.
TCP (Transport Control Protocol) is a connection-oriented protocol, that is, a reliable connection must be established with the other party before sending and receiving data. A TCP connection can be established only after three "conversations". The process is very complex. Only a simple process of the three conversations is described:
(1) first handshake: when a connection is established, the client sends the SYN Packet (SYN = J) to the server and enters the syn_sent status, waiting for confirmation from the server; SYN: synchronize sequence numbers ).
(2) second handshake: when the server receives the SYN packet, it must confirm the customer's Syn (ACK = J + 1) and send a SYN Packet (SYN = k) by itself ), that is, the SYN + ACK packet. At this time, the server enters the syn_recv state;
(3) third handshake: the client receives the SYN + ACK packet from the server and sends the ACK (ACK = k + 1) Confirmation packet to the server, the client and server enter the established status and complete the three-way handshake.
Understanding that three handshakes are required: TCP is a reliable transmission. Therefore, reliability must be ensured when a connection is established, and the response mechanism is essential. A and B need to complete a complete information interaction: A sends data to B, B must tell a to receive the data and give some information to, this leads to the second data transmission. Of course, a must also tell B that he has received the data, so a needs to send the data to B again. These three steps are the most basic process of information interaction. The whole process is a question-and-answer form.

Three handshakes are required for TCP connection establishment, while four handshakes are required for TCP connection disconnection:
(1) When host a completes data transmission, set the control bit fin to 1 and request to stop the TCP connection.
(2) host B responds to fin after receiving it, confirming that the upstream TCP connection of this party will be closed, and the ACK will be set to 1
(3) end B initiates a reverse request to close the fin.
(4) host a confirms the request of host B, sets Ack to 1, and closes the request in both directions.
It can be seen from the three-way handshake and four-way disconnection of TCP that TCP uses connection-oriented communication mode, which greatly improves the reliability of data communication, this enables interaction between the sending data end and the receiving end before the formal data transmission, laying a reliable foundation for the formal data transmission. From the four broken handshakes, we can see that only two steps are involved in B's reply to A and B's speech to a, instead of making a connection, in this way, a handshake occurs.

There are four reasons for this:
This is because the socket in the listen status of the server can respond to ack and SYN after receiving the SYN Packet connection request) it is sent in a message. However, when the connection is closed, when the other party receives the fin Message notification, it only indicates that the other party has no data to send to you; but not all your data may have been sent to the other party, therefore, you may not close the socket immediately, that is, you may need to send some data to the other party, and then send the fin message to the other party to indicate that you agree to close the connection now, therefore, the ACK messages and fin messages are sent separately in most cases.



UDP
(1) UDP is a non-connection protocol. The source end of data transmission does not establish a connection with the terminal. when it wants to transmit data, it simply crawls data from the application, and throw it to the network as quickly as possible. At the sending end, the speed of UDP data transmission is limited only by the speed at which the application generates data, the ability of the computer, and the transmission bandwidth. at the receiving end, UDP puts each message segment in the queue, the application reads a message segment from the queue each time.
(2) Since no connection is established for data transmission, you do not need to maintain the connection status, including the sending and receiving status. Therefore, a server can transmit the same message to multiple clients at the same time.
(3) The title of the UDP information package is very short, with only 8 bytes. The additional overhead of the 20 bytes information package of TCP is very small.
(4) throughput is not adjusted by the congestion control algorithm, but is limited by the data generation rate, transmission bandwidth, source end and terminal host performance of the application software.
(5) UDP uses the best effort to deliver, that is, it does not guarantee reliable delivery, so the host does not need to maintain a complex link status table (there are many parameters ).
(6) UDP is packet oriented. The packet sent by the sender's UDP to the application is delivered to the IP layer after the header is added. Without splitting or merging, the boundary of these packets is retained. Therefore, the application needs to select the appropriate packet size.
We often use the "ping" command to test whether the TCP/IP communication between the two hosts is normal. In fact, the "ping" command is used to send UDP packets to the other host, then, the host of the other party confirms that the packet is received. If the message indicating whether the packet arrives is returned in time, the network is connected.


Summary Differences Between TCP and UDP:
1. Based on connection and no connection;
2. Requirements on system resources (more TCP and less UDP );
3. Relatively 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.

Note: different explanations for points 4th:
When sending data over UDP, the maximum length of data that can be sent using the sendto function is 65535-20-8 = 65507 bytes, of which 20 bytes is the IP header length, the 8-byte UDP header length. When the sendto function is used to send data, if the data length is greater than this value, the function returns an error.
When sending using the TCP protocol, TCP is a data stream protocol, so there is no limit on the package size (the buffer size is not considered for the time being). This means that when using the send function, the data length parameter is not limited. In fact, the specified data segment is not necessarily sent at a time. If the data segment is long, it may be sent in segments. If the data segment is short, it may be waiting to be sent together with the next data.
Different interpretations:
Multi-thread scheduling, post-scheduling, post-scheduling, and different routing paths may cause the disorder of UDP data arrival.

Will packet loss occur when UDP transmission is used on a single machine?
A: Yes, but it does not. The destination IP address is the local IP address, or data packets using the loopback IP address directly return the IP input function before arriving at the data link layer. Therefore, this is a buffer issue, and the server processing speed cannot keep up.

The transmission process of TCP and UDP and the difference between them

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.