Categories
Uncategorized

TCP three-way handshake with the four waving

introduction

Some time ago has been preparing for interviews, I thought very good preparation, but when the interviewer asked Tencent network problems, discover their understanding of the TCP protocol really stay on the surface, not deep enough. So the spirit of improving their own ideas, to check the information, here is a summary of my main building understanding of TCP connection and disconnection process. You can see better layout here

Common topics

Network problems are in the interview will be examined, and the TCP protocol is the focus of study network knowledge. Frequently asked questions are as follows:

    Please talk about the process of establishing a connection of the TCP protocol

    Please describe the TCP three-way handshake protocol and fourth wave is kind of how

    Why should TCP three-way handshake protocol to establish the connection, not twice, not four times

    TCP connection is initiated by the syn No. Why not start from the same serial number, such as 1

Next, I will introduce TCP three-way handshake and four waving my understanding of the process, if the error also please correct me, thank you.

TCP protocol

Three-way handshake

首先需要服务器监听特定的端口,等待客户端来请求连接。当客户端需要建立连接时,客户端会先向服务器发送syn报文,将报文中syn置为随机生成的序号n(这里假设序号为1000)。服务器收到同步报文后,会回复一个ack报文,把ACK位置位n+1(这里的序号应该为1001),同时设置syn为y(这里假设为2000)。客户端收到服务器发送的ack报文后,会回复一个ACK报文给服务器,其中ACK位置为y+1(这里即为2001)。当服务器收到ACK消息后,即认为连接进入稳定状态。状态机与流程图如下:

Four waving process

When the client receives the off instruction from the app, client server will send a FIN message (indicating that client will not give data transmission server), client into the finish-wait-1 state. After the server receives a finish message, returns an acknowledgment message ack to the Client, into the close-wait state itself. After the client receives the ack message, into FIN-WAIT-2 state. And in this state waiting for the server to send messages finish. When the server receives a closing instruction app, server FIN message sent to the client. Server into the LAST-ACK state. After the client receives the FIN message, it replies with an ACK message, and proceeds to TIME-WAIT state, waiting to receive the ack message server, the client will remain TIME-WAIT state in the next 2MSL (maximum segment lifetime) time . Why is it 2MSL time, first, to have enough time server receives the ACK message and resend the message is lost. Second, in order to follow the end of this connection after the connection of the buffer provided. If MSL is not two times, then it is possible to mix packets from different connections, causing confusion message. Flow diagram of the state machine:

Complete the process

The following is a flowchart showing the complete TCP connection established to disconnect from

He began to answer

With the above description, basic able to answer the first two questions.

Please talk about the process of establishing a connection of the TCP protocol

look up

Please describe the TCP three-way handshake protocol and fourth wave is kind of how

look up

Why should TCP three-way handshake protocol to establish the connection, not twice, not four times

First of all it is simply a reliable connection does not exist, tcp only provide relatively reliable connection. The main object of the three-way handshake is required for communication parameter exchange, mainly the syn server and client number, this number is for transmitting and receiving data. If only two shook hands, then, when the server sends a message ack + syn, they would think to establish a stable connection, this time if ack + syn lost, client did not receive this message, the client will not think the connection is established success, and directly into the close state. This will result, server Shadeng which has been, never have to send client data, which will result in waste server resources. As for why not a four-way handshake, the handshake is because after three successful, you can identify the current connection is reliable, and otherwise also need client to send messages to each other between ack and server, so that the endless.

TCP connection is initiated by the syn No. Why not start from the same serial number, such as 1

Since network conditions in real unpredictable, such as when the client first connects to a number used for initial transmission data sequence number, transmitted data segments 1 to 30, this time because the network problems disconnected. The client is syn to 1 to re-establish a new connection, this time the server receives a 30 bytes before sending the data, the server will think that 30 bytes of data is emerging, which will lead to data confusion.

Reference material

Indicated, all the pictures are from The TCP / IP Guide.

References are as follows:

The TCP/IP Guide

Why TCP requires three-way handshake with the four waving

Detailed wave four times TCP three-way handshake

Leave a Reply