In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > IT Information >
Share
Shulou(Shulou.com)11/24 Report--
Everyone should have heard of the TCP and UDP protocols. Both TCP and UDP protocols work at the transport layer, and their goal is to transfer data between applications. Our commonly used network communications, such as browsing web pages, checking e-mail, telephone communications, and so on, are transmitted through these two protocols.
01. The difference between TCP and UDP what is the difference between these two protocols? How do they work? Follow the document and look down.
What is the core difference between TCP and UDP? Just take a look at the picture below.
TCP (Transmission Control Protocol, Transmission Control Protocol) is a connection-oriented, reliable, byte-stream based transport layer communication protocol. Reliability is the most important feature of TCP, which is mainly reflected in: no data loss, no error, no disorder, no repeat arrival. Just like the picture above, TCP can transmit the "child" (packet) to the receiver very safely.
UDP (User Datagram Protocol, user Datagram Protocol) is a connectionless, unreliable, fast transport layer communication protocol. Fast transmission is the most important feature of UDP, which is mainly reflected in: before data transmission, there is no need to establish a connection in advance, so data can be transmitted more efficiently, but the reliability can not be guaranteed. As pictured above, UDP is only responsible for sending out the "child" (packet), regardless of whether the recipient receives it or not.
After seeing the difference between the TCP protocol and the UDP protocol, let's see how the TCP protocol works.
02. How does TCP work? The connection management mechanism (reliable mechanism) of TCP communication is summarized as follows: three-way handshake to establish a connection, four waves to disconnect.
TCP is a connection-oriented protocol, so each request requires confirmation from the other party. The TCP client and the TCP server need to complete a three-way handshake to establish a connection before communicating.
First handshake
Purpose: this is a packet requesting to establish a connection. The client first sends a synchronous packet to the server.
The TCP header of the packet:
Synchronous SYN=1 (client requests to establish a connection)
Serial number seq=x (the serial number of the first byte of the data transmitted by the client)
The second handshake
Purpose: after receiving the first data packet sent by the client, the server judges the packet that is actively establishing a connection. If the server agrees to connect, the server sends a packet in response.
The TCP header of the packet:
Synchronous SYN=1 (server connection establishment)
Confirm the flag ACK=1 (the server agrees to connect)
Serial number seq=y (the serial number of the first byte of the data transmitted by the server)
Confirmation number ack=x+1 (has received x bytes of data from the client and told the client that it should start from the xth byte of the data next time)
The third handshake
Purpose: after receiving the confirmation from the server, the client sends a data packet to the server.
The TCP header of the packet:
Synchronous SYN=1 (both parties have agreed to establish a connection)
Acknowledgement flag ACK=1 (receipt of an acknowledgement packet from the server)
Serial number seq=x+1 (the packet sent is the xth byte of the data)
Confirmation number ack=y+1 (receives the server to send y bytes of data and tells the server to start from the first byte of the data next time)
Tips
In the TCP data transmitted by the client and the server, the values of the sequence number seq and the confirmation number ack of both sides are calculated on the basis of 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, it is impossible to continue the "handshake", thus ensuring the smooth completion of the "three-way handshake".
Eventually, when the data transfer is complete, the TCP client and the TCP server need to be disconnected through four sessions, a process we call four waves.
Wave for the first time
Purpose: the client sends the request packet released by the connection to the server and stops sending data.
In the TCP header where the connection releases the packet:
Terminate FIN=1 (the client actively releases the TCP connection between the client and the server)
The serial number seq=x (x) is specified by the client. Then wait for confirmation from the server)
The second wave.
Purpose: after receiving the message of connection release, 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 half-closed at this time. 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 acknowledgement message sent by the server to the client:
Acknowledge the flag ACK=1 (received the packet sent by the client and allow the client to release the connection)
Acknowledgement number ack=x+1 (on the basis of receiving the client message, add its sequence number seq value by 1 as the value of the message acknowledgement number ack in this paragraph)
The serial number seq=y (y) is specified by the server. Then wait for confirmation from the client)
Wave for the third time
Purpose: if the server has stopped sending data to the client, release the connection and send an acknowledgement message to the client.
In the TCP header of the confirmation message:
Terminate FIN=1 (the server releases the TCP connection from the server to the client and no longer sends data to the client)
Confirm the flag ACK=1 (the data transfer from the server to the client is complete)
Acknowledgement number ack=x+1 (on the basis of receiving the client message, add its sequence number seq value by 1 as the value of the message acknowledgement number ack in this paragraph)
The serial number seq=z (z) is specified by the server. Then wait for confirmation from the client)
Wave for the fourth time
Objective: after receiving the connection release message segment of the server, the client sends a confirmation message to the server.
In the TCP header of the confirmation message:
Terminate FIN = 1 (client and server disconnect)
The acknowledgement mark ACK=1 (receives an acknowledgement message from the server and agrees that the server releases the connection)
Acknowledgement number ack=z+1 (on the basis of receiving the server message, add its sequence number seq value by 1 as the value of the message acknowledgement number ack in this paragraph)
Serial number seq=x+1 (on the basis of receiving the server message, take its confirmation number as the value of the serial number of the message in this paragraph)
After learning about TCP, let's learn how UDP works.
03. How does UDP work? UDP protocol is connectionless, that is, there is no need to establish a connection before sending data (without TCP protocol complex three-way handshake, four-wave process). To send data is to simply encapsulate the data packet and then send it out from the network card, and there is no state connection between the data packets. The UDP protocol is basically the interface between the IP protocol and the upper layer protocol. The UDP protocol uses the services provided by the IP layer to transfer the data obtained by the application layer from an application process of one host to an application process of another host.
Give me a chestnut.
When the transport layer receives the UDP data message from the IP layer, according to the destination port in the header of the UDP data message, the UDP data message is delivered to the application process through the corresponding port, as shown in the following figure.
Such as application process 4 and port 2 in the figure above, if the receiver 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), it discards the message, and the ICMP sends a "port unreachable" error message to the sender.
Tips
ICMP (Internet Control Message Protocol,Internet Control message Protocol) is a sub-protocol of the TCP / IP protocol suite, which is used to transmit control messages between IP hosts and routers. The control message refers to the message of the network itself, such as whether the network is unreachable, whether the host is reachable, whether the route is available, and so on.
04. To sum up, data transmission seems simple, but it is actually very smart.
TCP and UDP serve programs, but programs are the same as people, and they can communicate with each other or talk to each other. TCP is honest communication with each other. There can be timely feedback on whether the message has been received and whether the information received is consistent.
Advantages: reliable.
Disadvantages: slow transmission rate.
Applicable scenarios: communication software account login, payment and other related functions, using reliable TCP.
UDP means to talk. I don't care whether you listen or not, whether you have feedback or not, I will keep talking.
Advantages: fast transmission rate.
Cons: unreliable.
Applicable scenarios: QQ, Wechat and other instant messaging software for point-to-point communication, or audio and video calls, usually use UDP with fast transmission.
TCP and UDP have their own advantages and disadvantages, depending on your actual needs.
For more practical information about TCP, please poke three handshakes and wave four times. TCP is so polite!
05. How to change UDP from unreliable transmission to reliable transmission?
Why is TCP a three-way handshake instead of two or four?
Why four waves instead of three?
This article comes from the official account of Wechat: ZTE documents (ID:ztedoc)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.