In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what the TCP protocol is. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
TCP protocol is a core protocol in TCP/IP system, which is more complex than IP protocol, ICMP protocol and UDP protocol, so this paper mainly analyzes the state transition and message segment content of TCP protocol when establishing and disconnecting.
Next, let's start with a state transition diagram of TCP:
Three-way handshake of TCP protocol
The process of three-way handshake is the process of TCP establishing a connection between the client and the server. To put it simply, the three-way handshake process is that the client sends a connection request to the server first, which is the first handshake. The server receives the link request from the client, and then sends the confirmation message to the client, which is the second handshake. The client acknowledges the confirmation message sent by the server, and then sends the confirmation message to the server, which is the three-way handshake. After a three-way handshake, the link is established.
Why does it have to be a three-way handshake to establish a reliable link? If it is not a three-way handshake, then after the client sends a link request, the server confirms the request and confirms that the link has been successfully established, commonly known as the second handshake. What are the disadvantages of this approach?
Consider such a situation, when the client shook hands for the first time, sent a message segment, but this message segment did not arrive because of network problems, at this time, the client will once again send a connection request message segment to the server, this time successfully received, the two establish a connection, and the communication ends, and closes the connection. After that, because the message segment of the network delay is sent to the server, and the server thinks that the client wants to establish a new connection, so it agrees and sends an acknowledgement to the client. Because it is a second handshake, the next thing the server needs to do is to wait for the message sent by the client, but the client will ignore the confirmation from the server, so the server will be waiting for the data from the client all the time. Wasted resources.
Grab the packet and analyze the message segment of TCP three-way handshake
Now, elaborate on exactly what the three-way handshake did.
In the first handshake, the client sends a message segment to the server, which is marked with the SYN flag, which indicates the establishment of a connection and an initial serial number.
In the second handshake, the server sends a message segment to the client, which is marked with the SYN flag and the ACK flag. The value of the ACK flag is the initial sequence number value + 1 sent by the client, which indicates that the client is confirmed, and the server has its own initial sequence number in the message segment.
During the third handshake, the client sends a message segment to the server, in which only the ACK flag is marked, and the ACK value is the initial serialization value of the server + 1, indicating that the server is confirmed, and there is also a sequence number value, which is the sequence number value + 1 of the client's first handshake.
Three-way handshake establishes the end of the connection.
There are three lines in the picture, each of which represents a handshake. Let's look at the first line first.
It can be seen that the program at port 55732 actively sends a linked message segment to port 8888 during the first handshake. This port 55732 is a Socket client written by the Java program, and port 8888 is the server written by the Socket program. In the message segment that establishes the connection, only SYN is 1 in the Flags, which indicates that this is a connected message segment, and the initial sequence number in this message segment is 0, and the number of ACK is also 0.
The second handshake is when the server sends a message segment to the client to confirm that the link request from the client has been received. There are two marks in the paragraph of the newspaper, one is ACK and the other is SYN. Indicates that the link request is received and the port is open. A server's own initial serial number is also sent in the message segment. Notice that the value of ACK becomes 1, which is the client's initial sequence number + 1.
The third handshake is when the client sends a message segment to the server, indicating that it has received the confirmation from the server. Because both ports have been opened, so the client is sending, there will be no more SYN flag, there is only the ACK flag, the value of this flag is also 1, because the initial sequence number of the server + 1. The sequence number here is 1, because the first handshake sends a SYN flag that takes up a serial number value, but ACK does not.
Corresponding to the above state chart, the client opens actively, sends a SYN message segment from the starting point, enters the SYN_SENT state, then accepts SYN,ACK, takes the path of black and thick lines to the data transmission state, that is, ESTABLISHED. For the server, it is the part of the dotted line from the starting point, passively opens, accepts the client's SYN, enters the SYN_RCVD, and finally accepts the ACK of the client's third handshake. Enter the data transfer state, that is, ESTABLISHED.
Four waves of the TCP agreement
TCP protocol is a full-duplex protocol with half-off feature.
Full-duplex means that A can send data to B, and B can also send data to A.
Half-duplex means that A can send data to B, and B can send data to A, but not both.
Simplex means that only A can send data to B, or B can send data to A.
Half-off means to turn full-duplex into simplex, that is, if B is closed, it means that B cannot send data to A, but A can send data to B.
So if the TCP protocol wants to close the link between the client and the server normally, it needs four message segments, that is, four waves.
First of all, the client will actively start to break the link because the execution of the application is complete, and a message segment containing the FIN flag bit will be sent. This indicates that the client will no longer send data to the server. This is the first time to wave.
Then, when the server receives the message segment, it sends a message segment with the ACK flag to the client, acknowledging that the closed message segment has been received. This is the second time to wave.
Then, after dealing with the business of the server, the server will also send a message segment containing FIN to the client, indicating that the server will no longer send data to the client. This is the third wave.
Finally, after the client receives this message segment, it will send a message segment containing ACK to the server to acknowledge that it has received the closed message segment. This is the fourth time to wave. At this point, all links are closed.
Grab the packet and analyze the message segment of TCP waving four times.
Instead of looking at the black message, four lines represent the process of waving four times off, and each line is a wave.
The first line is that the client actively closes the link and sends a message segment containing FIN, which is the first time to wave.
The second line, the server acknowledges the client's FIN message, sends an ACK confirmation message, the value of the ACK is 9, and the serial number of the first line is 8, because like SYN, FIN also occupies a serial number.
Ignoring the black third line, the server sends a message segment containing FIN, which is the third wave.
On the fourth line, the client acknowledges the FIN segment and sends an ACK segment. The value of the ACK is the serial number + 1 of the third line.
Corresponding to the above state chart, let's first talk about the state transfer of the server. Because the FIN of the client is received, an ACK is sent to the client. At the same time, you enter the CLOSE_WAIT state, wait for the server application to finish, send the FIN to the client, enter the LAST_ACK state, wait for the final ACK to arrive, receive the ACK, and end the state.
Because the client initiates the shutdown first, the status is complicated. He first sends a FIN to the server, and he enters the FIN_WAIT_1 state. At this time, he waits to receive the message from the server. There are three possibilities for the message:
Only ACK on the server side
Only FIN on the server side
Based on server-side ACK and FIN
For the first kind, the ACK is sent when the server confirms the FIN of the client, and the client enters the FIN_WAIT_2 state. When he receives the FIN from the server, he sends an ACK and enters the TIME_WAIT state. He has to wait for the 2MSL in this state. One MSL is the longest time the message segment is in the network, and the client waits for 2MSL so that when the last ACK is lost, it can be sent again. Because at this time, the server sends a FIN to the client after waiting for a timeout, the client also knows that the ACK is missing.
For the second method, when there is only the FIN of the server, an ACK is sent to the server, and when the client enters the CLOSING state, and then receives the ACK of the server, it also enters the TIME_WAIT state.
For the third type, if both are received at the same time, the CLOSING state is omitted and the TIME_WAIT state is directly entered. This is the case with the screenshot of the package analysis.
This is the end of this article on "what is the TCP Protocol?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.