6 Linux network programming TCP protocol (sequence number and acknowledgment number)
[Copy link]
This post was last edited by Rambo on 2018-6-22 09:38 The following content is the basis for learning subsequent content, and must be explained clearly first. In order to facilitate your recall of the TCP header, this picture is posted here again for comparison.
Figure 1 TCP header1. Sequence number1.1 The significance of sequence numberFirst, we need to understand why we need sequence numbers. In APUE basics, we send data to each other through the TCP protocol. For example, helloworld, this string of byte stream is assumed to be split into three TCP segments. The first segment carries hel, the second segment carries lowo, and the third segment carries rld. These three segments are not necessarily sent to the other end in order. So how does the other end determine the order of these three segments when it receives them? The significance of the sequence number is reflected here. 1.2 Sequence Number The sequence number occupies 4 bytes, that is, 32 bits. Its range is[0,2321][0,2321], that is, there are 4 294 967 296 sequence numbers in total. The sequence number in the TCP protocol refers to the segment sequence number. In a TCP connection, each byte in the transmitted byte stream (data) is numbered sequentially. That is to say, from the beginning of a TCP connection to the disconnection of the TCP connection, each byte of all the data you want to transmit must be numbered. This sequence number is called the byte sequence number. - Initial sequence number ISN
When a new connection is established, the sequence number of the first byte of data is called ISN (Initial Sequence Number), that is, the initial sequence number. ISN is not necessarily 1 at the beginning. According to the RFC (the document that specifies the network protocol), the allocation of ISN is based on time. When the operating system is initialized, there is a global variable g_number which is initialized to 1 (or 0) and then increases by 1 every 4us. When g_number reaches the maximum value, it wraps around to 0. When a new connection is established, the value of g_number is assigned to ISN. In the BSD system, this code implementation did not follow the protocol. It initialized g_number to 1 and increased by 1 every 8us, that is, it increased by 125000 every second. After about 9.5 hours, g_number wrapped around to 0. The initial sequence number is a very, very important concept. It tells the other end who is the first segment! The purpose of the three-way handshake is to confirm the initial sequence number, which will be discussed later. If the sequence number of a TCP segment is 301, and it carries 100 bytes of data, it means that the byte sequence number range of these 100 bytes of data is [301, 400]. The first byte sequence number carried by the segment is 301, and the last byte sequence number is 400.
Figure 2 A data packet captured in the previous experimentIn Figure 2, the segment sequence number is 2379453244, and it carries 6 bytes of data hello\0. These 6 bytes The data byte sequence numbers of the bytes are from h->2379453244, e->2379453245 to the last null character \0->2379453249. Note: The sequence number field is only meaningful in any of the following two situations: - The data field contains at least one byte
- This is a SYN segment, or a FIN segment, or a RST segment.
2. Acknowledgement numberIf you still remember the example of you and your partner sending text messages, this is not difficult. Every time you send a TCP segment, you have to wait for the other party to reply with a confirmation. However, this method is too inefficient. In the TCP protocol, the cumulative confirmation method is generally adopted, that is, each time multiple consecutive TCP segments are transmitted, only the last TCP segment can be confirmed. The other party replies with a confirmation number to indicate which TCP segment has been received. For example, if the sender sends a TCP segment with a segment number of 301, which carries 100 bytes of data, the receiver should reply with a confirmation number of 401, which means that the receiver has received the data with byte numbers [0, 400] and now expects you to send data with byte numbers 401 and later. The confirmation number field is only valid when the ACK flag is set. 3. The process of a complete TCP connection to release
Figure 3 This data packet is still captured in the previous experiment In order to clearly see the interaction process between the client and the server, it is drawn as the following timing diagram.
Figure 4 Interaction between client and server Now, we only need to observe how the other party responds after each TCP segment is sent. In order to facilitate the observation of the sequence number and acknowledgment number, I only retain the last three digits. 4. Summary - Byte sequence number and segment sequence number
- Initial sequence number
- Acknowledgment number
|