In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the knowledge points of linux TCP protocol". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the knowledge points of linux TCP protocol".
1. TCP protocol 1.1 TCP packet header
To understand the three-way handshake and four-wave hand, you first need to understand the structure of the TCP packet header, as follows:
Source port and destination port: 16 bits long, identifying the remote and local port numbers
Sequence number: SEQ,32 bit length, identifying the order of packets sent to prevent packet disorder
Confirmation number: 32 bits long, the response of the receiver to the TCP message segment sent by the sender, whose value is to add 1 to the sequence number of the received message, which is used to solve the problem of no packet loss
TCP header length: 4-bit header length, indicating how many 32bit tcp headers can have, that is, how many 4 bytes. Because the header length is 4 bits and the maximum can represent 15, the maximum TCP header length is 15 bytes, which means that the maximum TCP header length can be 60 bytes.
URG: indicates whether the emergency pointer is valid
The ACK:ACK location 1 table explicit recognition number is legal. If ACK is 0, the packet does not contain acknowledgement information, and the acknowledgement field is omitted.
PSH: prompts the receiving application to read the data from the TCP receive buffer immediately to make room for receiving subsequent data
RST: requires the other party to re-establish a connection, which is used to reset the wrong connection due to host crash or other reasons. It can also be used to reject illegal datagrams or connection requests.
SYN: indicates a request to establish a connection
FIN: notifies the other party that the connection is about to be closed
Window size: 16 bits long, is a means of flow control, this window, refers to the receiving notification window, it tells the opposite side of the local TCP buffer can hold how many bytes of data, so that the other side can control the speed of sending data
Checksum: 16 bits long, filled by the sender. According to this value, the receiver verifies whether the received TCP message segment is damaged during transmission. The checksum includes the header and the data part, which is an important guarantee for the reliable transmission of TCP.
Emergency pointer: 16 bits long, which is a positive offset, which, together with the sequence number, represents the sequence number of the next byte of the last emergency data. To be exact, it is the offset of the emergency pointer from the current sequence number. It is the way for the sender to send emergency data to the receiver.
Optional: the length of the TCP header can be up to 60 bytes, while the fields known in the first part occupy 20 bytes, so the options can be up to 40 bytes, including maximum TCP payload, window ratio, selecting duplicate datagrams, etc.
Note: TCP packets have no IP address, only ports
1.2 three-way handshake protocol
When using TCP to realize the communication between the source host and the destination host, the destination host must agree, otherwise the TCP connection cannot be established. In order to ensure the successful establishment of the TCP connection, TCP uses a method called three-way handshake, which makes the "serial number / confirmation number" system work properly, so that their serial numbers are synchronized. If the three-way handshake is successful, the connection is established and you can start transmitting data information. Three-way handshake: provides a reliable communication connection for applications, suitable for transferring large amounts of data at a time, and for applications that require a response. The three handshakes are as follows:
The TCP of the source host A sends a connection request message segment to the host B, and the SYN (synchronization) flag bit in the header should be set to 1, indicating that you want to establish a connection with the target host B, communicate, and send a synchronization sequence number X (for example: SEQ=100) for synchronization, indicating that the sequence number of the first data byte when the data is transmitted later is XSyn1, and the client status is SYN_SENT.
After the TCP of the target host B receives the connection request message segment, if it agrees, it will send back the confirmation. Reconfirm that the position of ACK should be 1. The confirmation number is Xreply 1, and a SYN=1 is also sent to An and a sequence number Y is sent, where the server side status is SYN_RCVD.
After receiving the confirmation from target host B, the TCP of source host A should give confirmation to target host B. Its ACK is set to 1, the confirmation number is Yicheng 1, and its serial number is Xerox 1. According to the TCP standard, the message segment with SYN set 1 consumes a sequence number, and when both sides are confirmed, the status is changed to ESTABLISHED.
The TCP of the source host A running the client process informs the upper application process that the connection has been established. When source host A sends the first Datagram segment to destination host B, its sequence number is still Xreply 1 because the previous acknowledgment segment does not consume the sequence number. When the TCP of the target host B running the service process receives the confirmation of the source host A, it also informs its upper application process that the connection has been established. So far, a full-duplex connection has been established.
1.3 tcp disconnected and waved four times
Tcp establishes a connection three times, but disconnects four times because tcp is full-duplex and needs to be closed in both directions.
When host A completes the data transmission, it sets the control bit FIN to 1 and sends a request to host B to stop the TCP connection, and the status is changed to FINWAIT1. At this time, the sequence number in the packet is the value of the acknowledgement number in the last packet sent by host B, and the acknowledgement number is the sequence number in the last packet sent by host A + the size of the data in the packet.
Host B responds to FIN after receiving it, confirming that the TCP connection in this direction will be closed, set ACK to 1, and the server status is changed to CLOSEWAIT, and the client status is changed to FINWAIT_1. In this case, the serial number is the confirmation number in the previous step, and the confirmation number is the serial number in the previous step plus 1.
The FIN is set to 1, the server status is LASTACK, the client status is TIMEWAIT, the sequence number is the confirmation number of the previous step, and the confirmation number is the sequence number of the previous step plus the size of the data carried by the packet.
Host A confirms the request of host B and sets ACK to 1. The closing end status of both directions is CLOSED, where the sequence number is the confirmation number in the previous step, and the confirmation number is the sequence number in the previous step plus 1.
Note: FIN, like SYN, consumes a sequence number. In theory, of the termination packets sent by the server when the TCP connection is closed, only the termination location is 1, and then the client acknowledges it. However, in the actual TCP implementation, in the termination packet, the acknowledgement bit and the termination bit are set to 1 at the same time, the acknowledgement position 1 indicates that the last transmitted data is acknowledged, and the termination position 1 indicates that the TCP connection in that direction is turned off.
1.4 what are the states of TCP
Generally, we can use netstat to view the current socket status.
CLOSED: indicates the initial state
LISTEN: indicates that a socket on the server side is listening and can accept connections
SYNSENT: status SYNSENT after the client sends the first SYN connection request during the three-way handshake
SYN_RCVD: also an intermediate state of the server during the three-way handshake
ESTABLISHED: indicates that the connection has been established. To be clear here, there is nothing really connected to the TCP connection. It just means that both parties are in ESTABLLISHED status, which means that the connection between the two sides is normal.
FINWAIT1: after a connection has been established, one of the parties requests to terminate the connection, waving four times in the middle
TIME_WAIT: it means that a FIN message has been received and an ACK message has been sent. Just wait for 2MSL (default is 2min) to return to the available state of CLOSED.
CLOSING: after sending the FIN message, you did not receive the other party's ACK message, but received the other party's FIN message instead. In this case, both parties shut down socket at the same time.
CLOSE_WAIT: four waves in the middle, indicating that you are waiting to close the connection.
LAST_ACK: passively turn off one party when waving four times. After sending the FIN message, wait for the other party's ACK confirmation message.
RST: open and close at the same time
1.5 Why did TIME_WAIT wait for 2MSL to become CLOSED?
There are two reasons:
Reliably terminate the TCP connection. In the last step of four waves, the acknowledgement packet sent by the client to the server may be lost, the server will resend the message segment, and the client will re-initiate the confirmation segment, which will require a period of time.
Make sure that the belated TCP message segments are identified and discarded. If you do not stay for a period of time, the port number of the original client can be immediately reused by another application. At this time, if the server has a belated message segment, it will be sent to the new application, which is obviously not allowed.
Note: the lifetime of a message segment is a MSL, so there will be no late message segments after 2MSL.
Thank you for reading, the above is the content of "what are the knowledge points of linux TCP protocol". After the study of this article, I believe you have a deeper understanding of what are the knowledge points of linux TCP protocol, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.