About three-way handshake protocol tcp and four waving understanding (one of the interview questions)

TCP communication process includes three steps:

1.建立TCP连接通道

2.传输数据

3.断开TCP连接通道。

As shown, it shows a schematic diagram of the TCP communication process.
Here Insert Picture Description

The figure consists of three parts: connection establishment, data transfer, is disconnected.

Establish a TCP connection is very simple, you can establish a connection through the three-way handshake. After establishing a good connection to transfer data. TCP data transmission involves a lot of concepts: retransmission timeout, fast retransmission, flow control, congestion control, and so on. Disconnect process is also very simple, four-way handshake to complete the process by disconnected. Three-way handshake to establish a connection:

The first handshake: a client sends syn packets (seq = x) to the server, and enters SYN_SEND state, waiting for the server to confirm; second handshake: server receives syn packets, must confirm the customer SYN (ack = x + 1) , while themselves sends a SYN packet (seq = y), i.e., SYN + ACK packet, then the server enters a state SYN_RECV; third handshake: the client receives the SYN + ACK packet to the server, the server sends an acknowledgment packet ACK ( ack = y + 1), this packet is sent, the client and server into the ESTABLISHED state, complete the three-way handshake.

Bag handshake does not include the transfer of data, after three-way handshake is completed, the client and server before the official start transferring data. Ideally, TCP connection, once established, before any party in the communication of the two parties take the initiative to close the connection, TCP connection will be kept down.

Data transmission process:

a.超时重传超时重传机制用来保证TCP传输的可靠性。每次发送数据包时,发送的数据报都有seq号,接收端收到数据后,会回复ack进行确认,表示某一seq 号数据已经收到。发送方在发送了某个seq包后,等待一段时间,如果没有收到对应的ack回复,就会认为报文丢失,会重传这个数据包。b.快速重传接受数据一方发现有数据包丢掉了。就会发送ack报文告诉发送端重传丢失的报文。如果发送端连续收到标号相同的ack包,则会触发客户端的快速重 传。比较超时重传和快速重传,可以发现超时重传是发送端在傻等超时,然后触发重传;而快速重传则是接收端主动告诉发送端数据没收到,然后触发发送端重传。c.流量控制这里主要说TCP滑动窗流量控制。TCP头里有一个字段叫Window,又叫Advertised-Window,这个字段是接收端告诉发送端自己 还有多少缓冲区可以接收数据。于是发送端就可以根据这个接收端的处理能力来发送数据,而不会导致接收端处理不过来。 滑动窗可以是提高TCP传输效率的一种机制。d.拥塞控制滑动窗用来做流量控制。流量控制只关注发送端和接受端自身的状况,而没有考虑整个网络的通信情况。拥塞控制,则是基于整个网络来考虑的。考虑一下这 样的场景:某一时刻网络上的延时突然增加,那么,TCP对这个事做出的应对只有重传数据,但是,重传会导致网络的负担更重,于是会导致更大的延迟以及更多 的丢包,于是,这个情况就会进入恶性循环被不断地放大。试想一下,如果一个网络内有成千上万的TCP连接都这么行事,那么马上就会形成“网络风 暴”,TCP这个协议就会拖垮整个网络。为此,TCP引入了拥塞控制策略。拥塞策略算法主要包括:慢启动,拥塞避免,拥塞发生,快速恢复。

关于tcp三次握手,通俗理解,客户端向服务器发送请求,这为第一次握手;服务器接收到客户端的请求,给以客户端输送响应,此为第二次握手;当客户端接收到服务器发送过来的响应后,再给客户端发送一个东西,没有任何意义,只是告知客户端我已经接收到你的响应,此为第三次握手。

四次挥手断开连接:

The first wave: Active close sends a FIN, for closing the active side to the closed side of the passive data transfer, that is, take the initiative to close the square to tell passive close side: I've made will not give you the data (of course, the fin before the packet data sent out, without receiving a corresponding acknowledgment message ack, still actively closed side retransmits the data), but this time the active close side may further accept data.

Second wave: passive shutdown has received the FIN packet, sends an ACK to each other, for the receipt of the acknowledgment number No. + (the same SYN, FIN a occupy a sequence number).

Third Wave: Passive closed sends a FIN, for closing the passive side to take the initiative to close the closed side of the data transfer, that is, to tell the initiative to close the party, I also send the data over, will not give you send the data.

The fourth wave: the initiative has received close after FIN, ACK to send a passive closed party, numbered receipt confirmation number +1 This completes the four wave.

Released four original articles · won praise 0 · Views 19

Guess you like

Origin blog.csdn.net/weixin_40740993/article/details/104565017