Three TCP handshake protocol of the fourth wave

TCP is a reliable transport protocol: performance in two aspects

1 is to ensure that data packets can be transmitted in the order of arrival

2. On the other hand is to ensure the correctness of data packets to a certain extent (later explain why a certain degree of correctness).

Reliability based on the realization technology 2:00,

That is having a CRC check, so that if some of the data packets, an error can be found by the checksum;

Another point is that each packet has a sequence number in order to assure the order data packet, if the misalignment occurs may request retransmission of the data packet.

 

TCP protocol is a reliable transport protocol, reliability performance in two aspects, one is to ensure that data packets can be transmitted in the order of arrival, on the other hand is to ensure a certain degree of correctness of a data packet (hereinafter explain why some extent correctness). Implementations based on reliability 2:00 technology, that is having a CRC check, so that if some of the data packets, an error can be found by the checksum; Another point is that each packet has a sequence number, this ensures that the order of data packets, if the misalignment occurs may request retransmission of the data packet.

Now comes the format, data format that we look at the TCP packet. FIG follows the format of the TCP packet, including the original port, destination port, and serial number, etc. SUMMARY flag, some more content, watching can be somewhat blurred. However, a large aspect understood that this packet actually contains only part 2, it is a header, the other is the specific data to be transmitted. Control logic TCP protocol, the header plays the most crucial role, such as it is the TCP protocol connection is established, disconnects, various characteristics of the basic error checking and retransmission.

TCP packet format

2 TCP packet format of FIG.

Other meanings of header information are relatively clear, this article only describes the meaning of a few flag (URG, ACK, PSH, RST, SYN and FIN) of. Specific meaning as follows:

  • ACK: acknowledgment number is valid.
  • RST: Reset the connection
  • SYN: initiate a new connection
  • FIN: the release of a connection

Establishing a connection

Before specific TCP to establish a connection to transfer data. The connection here is not the physical connection, the physical connection based on the underlying protocol has been established, and TCP connection is established to assume that the underlying connection has been successful, the TCP connection is actually a virtual, logical connection. Brute appreciated that the client and server are recorded the number of each received packet, and sets itself to a status. In the TCP protocol, establishing a connection usually becomes 3-way handshake can be seen from the literal concept of establishing a connection to go through the confirmation process three times.

3-way handshake to establish a connection

3 3-way handshake to establish a connection

3 Protocol TCP handshake procedure shown in FIG., The initial state of the client and server are in the closed state. The main process is divided into three steps:

  • The client sends a pre-connection data packets: TCP connection is initiated by the client actively established, the client sends a packet (message) to the server, note that SYN packet flag is 1. We've covered previously, if the SYN is 1, then to establish a connection packet. Meanwhile, the request comprises a sequence number in the packet, the sequence number is also the basis for establishing the connection.
  • Server connection confirm reply: Confirm the server can establish a connection (not necessarily the server can establish a connection, because the number of sockets in the system is limited) will send a response packet to the client in the case. In the response packet ACK flag will be set to 1, that the response packet to the server. Meanwhile, in the response packet will set the value of the sequence and acknowledgment request sequence number, with particular reference to FIG.
  • The client connection confirmation reply: ***, the client sends a connection confirmation packet again, tell the server connection is established.

As can be seen from the above process, the need to establish a connection through multiple interactions, this is what we say every day in the high cost of the operation is to establish a connection. In the actual production environment, in order to deal with this problem, it will reduce the frequency of connection establishment, the usual practice is to establish a connection pool, access to the transmission of data directly from the connection pool connection, rather than creating a new connection.

Some may feel that we can optimize the process of establishing the connection, such as a client *** cancel confirmation, that this does not use eggs. For the normal case does not have much effect, here mainly deal with unusual circumstances. Because the network topology is very complex, especially in the WAN, with numerous network nodes, so there will be a variety of unusual circumstances. Therefore, TCP protocol in the design must ensure reliability under unusual circumstances.

我们这里举一个例子,就是连接请求超时的情况。假设客户端向服务端发送一个连接请求,由于各种原因,请求一直没有到达服务端,因此服务端也就没有回复连接确认消息。客户端连接超时,因此客户端重新发送一个连接请求到服务端,这次比较顺利,很快到达了,并且顺利建立了连接。之后,前一个数据包经过长途跋涉最终还是到了服务端,服务端也向客户端发送了回复数据包,服务端认为连接是建立成功的,并且会维持连接。但客户端层面认为连接是超时的,因此将永远不会关闭该连接。这样就会造成服务端有残留的资源,从而造成服务端资源浪费,久而久之可能会导致服务端无新连接资源可用。

另外一个需要说明的是客户端和服务端的套接字都有相应的状态,而且状态会随着连接的不同阶段变化。初始状态都是CLOSE,最终连接建立成功后都是ESTABLISHED,具体变化过程如图3所示。后面本文会详细介绍状态变化情况。

传输数据

完成连接建立之后,客户端和服务端就可以进行数据传输了。我们知道TCP是可靠的传输,那么传输的可靠性是通过什么来保证的呢?主要就是通过包头中的校验和、请求序列号和应答序列号(参考图2)。

TCP数据内容的可靠性是通过校验和保证的。TCP在发送数据时都会计算整个数据包的校验和,并存储在包头的校验和字段中。接收方会按照规则进行计算,从而确认接收到的数据是否是正确的。发送发计算校验和的流程大概如下:

  • 把伪首部、TCP包头和TCP数据分为16为的字,并把TCP包头中的校验和字段置0
  • 用反码加法累加所有16位数字
  • 对计算结果去反,将其填充到TCP包头的校验和字段

接收方将所有原码相加,高位叠加,如果全为1则表示数据正确,否则说明数据有错误。

TCP数据包顺序的可靠性是通过请求序列号和应答序列号保证的。在数据传输中的每个请求都会有一个请求序列号,而在接收方接收到数据后会发送一个应答序列号,这样发送方就能知道数据是否被正确接收,而接收方也能知道数据是否出现乱序,从而保证数据包的顺序性。

断开连接

Close TCP connection is divided into four steps, it referred to 4 sway. Close the connection is not necessarily in the client initiated, the server can initiate the connection is closed. Close the connection process is as follows:

  • Initiator sends a FIN packet data set for a request to close the connection to the sender the recipient
  • The receiver returns a response, the ACK flag bit is 1, confirmation closed. This completes the initiator is connected to the receiver, i.e. the sender can no longer transmit data recipient, and the recipient can also send data to the sender.
  • After the completion of the data transfer recipient to the initiator transmits a FIN packet to 1, indicates disconnection.
  • Originator return an ACK packet to confirm a successful close

Connecting a schematic flowchart Close

4 schematic flowchart of FIG close the connection

TCP is a full-duplex communication, thereby closing off the connection requires a two-way connection. The first is to close off this end of the initiator of the connection, and then close off the receiver after receiving the request initiator, in addition to off response replies, but also to ensure that initiates a request to close the connection after the data transfer is completed, to ensure a two-way closed simultaneously.

Guess you like

Origin www.cnblogs.com/hujc/p/11770911.html