Computer network-----TCP three-way handshake related interview questions expansion

1. TCP three-way handshake

直接上图 TCP三次握手

Insert picture description here

一些专业缩写单词

Explanation
TCP Transmission Control Protocol (TCP, Transmission Control Protocol)
SYN Synchronization sequence number
SYN_SENT Client request connection
ESTABLISHED Meaning TCP: successful connection
ACK (Acknowledge character) is the confirmation character. Only when ACK=1, the confirmation number field is valid. TCP stipulates that ACK must be set to 1 in the transmission of all messages after the connection is established;
SYN_RCVD SYN_RCVD is the intermediate state of the TCP three-way handshake. It is the state of the service port (monitoring port, such as port 80 of the application server) after receiving the SYN packet and sending the [SYN, ACK] packet
RST One of the 6 flag bits in the TCP header indicates that the connection is reset and the connection is reset. Reset RST. When RST=1, it indicates that there is a serious error in the TCP connection. The connection must be released and then re-established;
ISN Initial Sequence Number (Initial Sequence Number)
END To release the connection. When FIN=1, it indicates that the data of the sender of this message has been sent and requested to be released;
  • 最开始的时候客户端和服务器都是处于CLOSED状态。主动打开连接的为客户端,被动打开连接的是服务器。

第一次握手

  • The client client assigns SYN( 同步序列编号) to 1, and randomly generates an initial sequence number and seqsends it to the server server, entering the SYN_SENT state.
  • SYN_SENTIndicates that a connection is requested. When you want to access the services of other computers, you must first send a synchronization signal to the port. At this time, the status is SYN_SENT. If the connection is successful, it will becomeESTABLISHED

第二次握手

  • After the Server server receives the SYN (synchronization sequence number) of the client, it knows that the client is requesting to establish a connection, and assigns its SYN to 1, ACK to 1, and generates an acknowledgement number=sequence number+1, and randomly Generate an initial serial number of its own and send it to the client. The server enters the SYN_RCVD state; the client enters the ESTABLISHED state.

第三次握手

  • The client checks whether the acknowledge number is the serial number +1 and the ACK is 1, and after the check is correct, it sets its own ACK to 1, generates an acknowledge number = the serial number sent by the server +1, and sends it to the server; enters the ESTABLISHED state; the server After checking that the ACK is 1 and the acknowledge number is the sequence number +1, it also enters the ESTABLISHED state; the three-way handshake is completed, and the connection is established.

2. Can TCP three-way handshake become two-way handshake?

  1. Can't
  2. It may happen that the invalid connection request segment is transmitted to the server.
	client 发出的第一个连接请求报文段并没有丢失,而是在某个网络结点长时间的滞留了,以致延误到连接释放以后的某个时间才到达 server。
	本来这是一个早已失效的报文段。但 server 收到此失效的连接请求报文段后,就误认为是 client 再次发出的一个新的连接请求。
	于是就向 client 发出确认报文段,同意建立连接。假设不采用 “三次握手”,那么只要 server 发出确认,新的连接就建立了。
	由于现在 client 并没有发出建立连接的请求,因此不会理睬 server 的确认,也不会向 server 发送数据。
	但 server 却以为新的运输连接已经建立,并一直等待 client 发来数据。但此时的客户端早已进入CLOSED状态,服务端将会一直等待下去,
	这样浪费服务端连接资源.采用 “三次握手” 的办法可以防止上述现象发生。例如刚才那种情况,client 不会向 server 的确认发出确认。
	server 由于收不到确认,就知道 client 并没有要求建立连接。
  1. The two handshake cannot guarantee that the client correctly receives the message of the second handshake (the Server cannot confirm whether the client has received it), nor can it guarantee the successful exchange of initial serial numbers between the client and the server.

3. Can TCP use four-way handshake?

  1. can. But it will reduce the transmission efficiency.

  2. The four-way handshake refers to: the second handshake: Server only sends ACK and acknowledge number; and the server's SYN and initial sequence number are sent during the third handshake; the third handshake in the original protocol becomes the fourth handshake. For optimization purposes, two and three of the four-way handshake can be combined.

4. In the third handshake, what happens if the client's ACK is not delivered to the server?

  1. Server side:
	由于Server没有收到ACK确认,因此会重发之前的SYN+ACK(默认重发五次,之后自动关闭连接进入CLOSED状态),
	Client收到后会重新传ACK给Server。
  1. Client side, two situations:
	1. 在Server进行超时重发的过程中,如果Client向服务器发送数据,数据头部的ACK是为1的,所以服务器收到数据之后会读取 ACK number,进入 establish 状态
	2. 在Server进入CLOSED状态之后,如果Client向服务器发送数据,服务器会以RST包应答。

5. What should I do if the connection has been established but the client fails?

  1. The server resets a timer every time it receives a request from the client. The time is usually set to 2 hours. If it has not received any data from the client for two hours, the server will send a probe segment, and then every 75 Sent once every second. If there is no response after sending 10 probe messages, the server considers the client to be faulty, and then closes the connection.

6. What is the initial serial number?

  1. One party A of the TCP connection randomly selects a 32-bit sequence number (Sequence Number) as the initial sequence number (ISN) of the sent data, such as 1000, and uses the sequence number as the origin to perform the data transmission Number: 1001, 1002... During the three-way handshake, send this initial serial number to the other party B, so that when transmitting data, B can confirm what data number is legal; at the same time, when transmitting data, A can also confirm For every byte that B receives, if A receives B's acknowledgement number (acknowledge number) is 2001, it means that the data numbered 1001-2000 has been successfully accepted by B.

Guess you like

Origin blog.csdn.net/weixin_45480785/article/details/114136987