Everyone should have heard of TCP and UDP. Both TCP and UDP work at the transport layer, and their goal is to transfer data between applications. Our common network communications, such as browsing the web, checking emails, and making phone calls, all use these two protocols to transfer data. 01. The difference between TCP and UDPWhat is the difference between these two protocols? How do they work? Follow the document below~ What is the core difference between TCP and UDP? You will know it by looking at the picture below. TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte-stream-based transport layer communication protocol. Reliability is the biggest feature of TCP, which is mainly reflected in: no data loss, no errors, no disorder, and no duplicate arrival. As shown in the figure above, TCP can transmit the "children" (data packets) to the receiver very safely. UDP (User Datagram Protocol) is a connectionless, unreliable, fast-transmission transport layer communication protocol. Fast transmission is the biggest feature of UDP, which is mainly reflected in: no need to establish a connection in advance before sending data, which can transmit data more efficiently, but reliability cannot be guaranteed. As shown in the figure above, UDP is only responsible for sending the "child" (data packet), regardless of whether the receiver has received it. After intuitively feeling the difference between TCP protocol and UDP protocol, let's take a look at how TCP protocol works? 02. How does TCP work?The connection management mechanism (reliable mechanism) of TCP communication can be summarized as: three handshakes to establish a connection and four handshakes to disconnect. <Three-way handshake to establish a connection> TCP is a connection-oriented protocol, so each request needs to be confirmed by the other party. The TCP client and the TCP server need to complete a three-way handshake before communicating to establish a connection. First HandshakePurpose: This is a data packet requesting to establish a connection. The client first sends a synchronization data packet to the server. The TCP header content of the data packet:
Second handshakePurpose: After receiving the first data packet sent by the client, the server determines that it is a data packet actively establishing a connection. If the server agrees to connect, the server sends a data packet in response. The TCP header content of the data packet:
The third handshakePurpose: After the client receives confirmation from the server, it sends a data packet to the server. The TCP header content of the data packet:
TipsIn the TCP data transmitted between the client and the server, the values of the sequence number seq and the confirmation number ack of both parties are calculated based on each other's seq and ack values, which ensures the continuity of TCP data transmission. Once the TCP data sent by one party is lost, the "handshake" cannot continue, thus ensuring the smooth completion of the "three-way handshake". < Wave four times to disconnect > There is always a time to meet and part. After the data transmission is completed, the TCP client and the TCP server need to disconnect through four sessions. We call this process four waves. First wavePurpose: The client sends a connection release request packet to the server and stops sending data. In the TCP header of the connection release packet:
Second wavePurpose: After receiving the connection release message, the server sends a confirmation message to the client. Therefore, the connection from the client to the server is released, and the TCP connection is in a half-closed state. Half-closed because the client can no longer send data to the server, and half-open because the server can still send data to the client and be received by it. In the TCP header of the confirmation message sent by the server to the client:
The third wavePurpose: If the server has stopped sending data to the client, it releases the connection and sends a confirmation message to the client. In the TCP header of the confirmation message:
The fourth wavePurpose: After receiving the connection release message segment from the server, the client sends a confirmation message to the server. In the TCP header of the confirmation message:
After understanding TCP, let's learn how UDP works~ 03How does UDP work?UDP protocol is connectionless, that is, it does not need to establish a connection before sending data (no complicated three-way handshake and four-wave process of TCP protocol). Sending data is simply to encapsulate the data packet and then send it out from the network card. There is no state connection between the data packets. UDP protocol is basically the interface between IP protocol and upper layer protocol. UDP protocol uses the service provided by IP layer to transfer the data obtained by application layer from an application process of one host to an application process of another host. For example~ When the transport layer receives a UDP datagram from the IP layer, it passes the UDP datagram to the application process through the corresponding port based on the destination port in the UDP datagram header, as shown in the following figure. As shown in the above figure, application process 4 and port 2, if the receiving UDP finds that the destination port number in the received message is incorrect (that is, there is no application process corresponding to the port number), the message will be discarded and ICMP will send a "port unreachable" error message to the sender. TipsICMP (Internet Control Message Protocol) is a sub-protocol of the TCP/IP protocol suite, used to transmit control messages between IP hosts and routers. Control messages refer to network messages such as whether the network is connected, whether the host is reachable, and whether the route is available. 04SummaryData transmission seems simple, but it is actually very smart~ TCP and UDP serve programs, but programs are the same as people. They can communicate with each other and talk to each other. TCP is about honest communication between each other. Whether the message is received or not, and whether the received information is consistent, can all be fed back in time. Advantages: Reliable. Disadvantages: Slow transmission rate. Applicable scenarios: Account login, payment and other related functions of communication software, using reliable TCP. UDP means talking. I don’t care whether you listen to me or not, and whether you give me feedback or not. I will keep talking. Advantages: fast transmission rate. Disadvantages: Unreliable. Applicable scenarios: QQ, WeChat and other instant messaging software usually use UDP with fast transmission speed for point-to-point communication or audio and video calls. TCP and UDP each have their own advantages and disadvantages, it depends on your actual needs~ For more TCP tips, please click on the three-way handshake and the four-way wave. It turns out that TCP is so polite! |
>>: A brief discussion on Bluetooth mesh technology and its application in smart home field
The tribe has shared a lot of cheap RackNerd VPS ...
Whether it is the turbulent forty years in Wu Xia...
2019 is the first year of 5G commercial deploymen...
Linode is a VPS cloud hosting company that has be...
Five or six years ago, the rumor that "cloud...
Just for record, I found that there are still hug...
In the blink of an eye, the summer vacation is ha...
[[259267]] Recently, the Fuzhou Radio Management ...
With the advent of the Internet of Things era, th...
Traditional WANs can no longer keep up. In the br...
We have previously shared information on VPS host...
This article is reproduced from Leiphone.com. If ...
[51CTO.com original article] At 9:30 am on March ...
Since the three major operators in my country occ...
In 2017, the global 5G mobile communications era ...