Hi, I’m Hong Jelduck, and the next few articles are about TCP.
Why talk about TCP? Because Hongjak found that almost all the interview questions revolve around TCP, or extend from TCP to ask, for example, three handshakes. Is two handshakes ok? Four waves, why a TIME WAIT? Flow control, congestion control, sliding Windows and so on. So, I want to spend a few articles to walk you through the basics of TCP.
All the functionality of TCP is embodied in the fields in its header, soYou can understand the working principle of TCP only by understanding the functions of each field in the TCP header, so Hong Jue would like to start from the first TCP explanation, we should pay attention to oh, start the train!
Description of the fields in the TCP header:
(1) source port and destination port, read hongjue UDP articles know, this is used to represent the communication process of the port number, each account for 2 bytes.
(2) serial number, accounting for 4 bytes, the range of serial number can be calculated [0, 2^32-1], when 2^32-1 is needed, its next serial number will start to generate from small to large, that is, the final serial number = serial number % 2^32. TCP is byte stream oriented. The first sequence number is generated when establishing a TCP connection. It does not necessarily start at 0, but it is a random value. Each byte in the byte stream transmitted by TCP is assigned an ordinal number. The ordinal number field in the header refers to the ordinal number of the first byte of the data sent in the packet. We, for example, for example newspaper articles section of A serial number field value is 201, the message section with A 100 bytes of data, it is indicated that the message section of the first byte of A serial number is 201, the last to 300 bytes, so if it still has the next message segment B, then the message section B of the serial number is 301.
(3) The confirmation number, which also occupies 4 bytes, is the serial number of the first data byte of the next message segment expected to be received from the other party. As in the example above, segment A sends data with serial number 201 to the receiver. The data length is 100 bytes, which indicates that the receiver has received data with serial number 201 to 300. In this case, the receiving end expects the serial number of the next packet segment to be 301. Therefore, the receiving end sets the confirmation number in the acknowledgement packet segment to 301. In general: if the confirmation number is K, then all data up to k-1 has been correctly received.
(4) Data offset, occupy 4 bits, it is used for what? It indicates the distance between the start of the TCP packet and the start of the TCP packet. The length of the header of the TCP packet segment is the same as that of the header of the TCP packet segment. But because the length of the first field and uncertainty options field, so it is necessary to offset field data, and the unit of data migration is a 32-bit word (4 bytes), conversion, 4 who said decimal 0 to 15, so the data migration can be said of a maximum of 15 * 4 = 60 bytes, this is the maximum length of the TCP header, That is, the option length cannot exceed 40 bytes.
(5) Reserved, this field accounts for 6 bits, reserved for future use, is set to 0 at present.
(6) The next six control bits represent the nature of the current message segment.
Emergency URG, when URG = 1, it indicates that the emergency pointer field is valid. One of its main function is to show the current message segment have emergency data, need to transfer (high priority data), as soon as possible and do not need to queue for transmission, it reduces the emergency data inserted into this article, the front of the piece of data and the data is still behind the emergency data is normal data, the need to be used in conjunction with the emergency in the first pointer.
ACK: The ACK number field is valid only when ACK = 1. ACK = 0 indicates that the ACK number is invalid. TCP specifies that after a connection is established, ACK must be set to 1 for all transmitted packets.
Push PSH. When two application processes are communicating in an interactive manner, the other side is expected to respond immediately after one end outputs a command. In this case, TCP can use push operation, the sender sets PSH to 1, the receiver receives the packet segment PSH = 1, and delivers the response as soon as possible, without waiting for the entire cache to fill up.
Reset RST, when RST = 1, indicates that there is a serious error in the TCP connection, must release the connection, and then re-establish the transport connection. RST set to 1 has another function, which is to reject an invalid message segment or to refuse to open a connection.
SYN = 1 and ACK = 0 indicate that this is a connection request segment. If the peer agrees to establish a connection, SYN = 1 and ACK = 1 in the response packet segment. Therefore, a SYN value of 1 indicates that this is a connection request or connection accept message.
Terminates the FIN to release a connection. If the FIN value is 1, the sender finishes sending data and requests to release the transport connection.
(7) window, accounting for 2 bytes, so window values are integers between [0, 16^ 2-1]. Window refers to the receiving window (not the sending window!!) of the party that sends the message. , this field is intended to tell the sender the amount of data (in bytes) that the sender is currently allowed to send, starting with the confirmation number at the top of the column. Because each side’s data cache space is limited, the window value is used as a basis for the receiver to let the sender set its sending window, so the window value can change dynamically frequently.
(8) test and, accounting for 2 bytes, test data and field including the first and two parts, and inspection and almost the same as in UDP, you can see the second article, 【 】 【 】 【 】 】 【 TCP to generate a 12 bytes of pseudo first, but the difference is that the fourth field pseudo first need from 17 to 6 (UDP protocol number is 17, The TCP protocol number is 6), and the UDP length in the fifth field is changed to TCP length.
(9) Emergency pointer, accounting for 2 bytes, emergency pointer is only meaningful when URG = 1, it indicates the number of bytes of emergency data in this paragraph (after the end of emergency data is ordinary data). The function of the emergency pointer is to indicate the position of the end of the emergency data in the packet segment. (Window 0 can also send emergency data)
(10) Option, variable length, up to 40 bytes. When no option is used, the length of the TCP header is 20 bytes. So what data does the option contain? TCP initially provides only one of the following options: Maximum Segment Size (MSS) Maximum Segment Size. Note that this refers to the maximum length of the data field in each TCP packet segment. The data field plus the TCP header is equal to the entire TCP packet segment. If the MSS is too small, the network utilization decreases. If the MSS is too large, it needs to be sharded at the IP layer. Therefore, it is ok to set the MSS as not sharded at the IP layer as possible. Specific UDP articles are described in detail, here will not repeat the explanation. Later, with the development of the Internet, some new options were added, such as window enlargement, timestamp, SACK option, etc.
(11) padding, padding only to make the entire TCP header length is an integer multiple of 4 bytes. If it happens to be a multiple of 4 bytes, no padding is required.
Ok, we have covered the meaning of each field in TCP, hope that after reading this article partners can have a deeper understanding of TCP! Ok, we are ready to start TCP’s journey! Look forward to Hong Jue’s next article!
May everyone read this article with skepticism and explore how it works.
Road obstruction and long, the past for preface, the future for chapter.
Looking forward to our next meeting!