Operation and maintenance interview questions about the "TCP three-way handshake" several "whys"

Operation and maintenance interview questions about the TCP three-way handshake several "why"

1. Could you please tell us the structure of TCP packets?

  • TCP is at the transport layer and handles port-to-port communication. Therefore : the starting source port number and destination port number of the message header;
  • The 32-bit sequence number Seq, in bytes , refers to the sequence number of the first byte at the beginning of the data part of the message;
  • The 32-bit acknowledgment sequence number ack, the unit is byte , which means that it is hoped that the other side port will send data from the first byte;
  • 4-bit header length, range 0-15, unit is 4 bytes , the header length is up to 60 bytes, but because the header has necessary message identification information and protocol content constraints (context), it is agreed that at least 20 bytes ;
  • 6 bits are reserved, set to zero, and reserved for future generations.
  • Function bits: URG, ACK, PSH, RST, SYN, FIN
    • URG: Urgent bit, which identifies whether the data is urgent or not, and is used in conjunction with the data pointer at the head and tail, that is, the byte from the first byte to the byte specified by the urgent pointer is urgent data and should be transmitted as soon as possible.
    • ACK is not the previous ack. Please remember that only the ACK is 1 and the acknowledgment number field is valid, and the ack is invalid if the acknowledgment number is 0; so after someone asks you how much ACK is after the TCP connection is established, will you answer? Of course it is 1.
    • PSH: The transport layer is handed over to the receiving application process as soon as possible, no longer waits until the entire cache is filled with the transport layer to deliver it upwards, so do you understand who this bit is for ? That's right, the transport layer at the receiving end.
    • RST: Reset bit (so awkward!). When RST=1, when there is a serious connection error during the TCP connection, the connection must be released, and then the transport layer TCP connection must be re-established. So do you know when this bit is useful ? That's right, after the TCP connection process and the connection is successful ! During the connection process, the third handshake fails , and the receiver must send a RST=1 message to forcefully close this TCP connection! What happens after the connection is successful? One of the two sides of the output transmission appeared, the host crashed, etc., this bit also came in handy! This person is often asked and must be memorized.
    • SYN: Synchronization bit, 1 means that this packet is a connection request (first handshake), or connection reception message (second handshake), understand? After the connection is successful, or the third handshake, this is 0.
    • FIN: The termination bit is used to release the connection from one party to the other, so I ask you, in the course of four waves of hands, the message with FIN=1 appeared several times and it was correct twice, because it is full-duplex communication. One party should tell the other party that I will not send you any new data.
  • Window field: 2 bytes. Point out how many bytes of data the other party can send from the current transport layer buffer space . So I ask you, does this **cache data include the header? **Of course it is not included!
  • Checksum: 2B, check the header and data. Memorize
  • Urgent pointer: 2B, indicating how many bytes of urgent data there are, counting from the data part.
  • Option field: variable length.
  • Data part.
    Insert picture description here

2. Could you describe the process of the three-way handshake?

Insert picture description here

  • The first handshake : The client (Client) sends the first packet to the server (Server) with the flag bits: SYN=1, ACK=0 , and the sequence number Seq=x. The client ( original CLOSED state ) enters: SYN-SEND state , waiting for the server to confirm;

  • The second handshake : The server receives the packet sent by the client, and then sends the second packet. The packet's SYN=1, ACK=1 , the receiving sequence number ack is x+1, the sending sequence number Seq is y, and the server enters SYN -RCVD status;

  • The third handshake : After receiving the packet from the server, the client sends the last packet to the server. The flag bit SYN=0, ACK=1 , the sending sequence number Seq is x+1, and ack is y+1. After this packet is sent, both the client and the server enter the ESTABLISHED state one after another . So far the three-way handshake is complete.

3. Why is there a third handshake instead of two handshake?

  • The two-way handshake only allows one party to establish an ISN , while the other party acknowledges it. This means that only one party can send data .
  • TCP is a two-way communication protocol , which means that either end should be able to send data reliably. Both parties need to establish an ISN (that is, a randomly generated initial sequence number ), and both parties need to recognize the other's ISN and tell each other this value to achieve synchronization between the two parties and ensure reliable transmission
  • In short, one party must confirm the seq sequence number sent by the other party to be considered a one-way connection. So for example, both parties agree that at least three handshake is required, understand?

4. What happens if the third handshake fails?

  • After the receiving end has been in the SYN-RCVD state, it has not received the ACK message. If it times out , it will not retransmit the ack message. Instead, it will directly send the message segment with RST=1 , enter the CLOSED state , and start again. Enter the LISTEN state.
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_31789689/article/details/108208865