TCP three-way handshake process

We all know that computers and network devices to communicate with each other, both sides must be based on the same method, different hardware, communication between the operating system, all this requires a rule. We call this rule is called protocol.

TCP / IP is related to all types of Internet protocol suite in general. TCP / IP refers to both TCP and IP protocols. TCP / IP is used during the communication to the IP protocol in the protocol family collectively.

Attachment: a computer process to access the Internet
Here Insert Picture Description
can be seen, when using TCP / IP suite of protocols to access the network, it will be communicated with each other by sequentially layered.
Sequence:
sending end: application layer (HTTP client) → the transport layer (the TCP) → the network layer (the IP) data link layer →
the server side: a data link layer → the network layer (the IP) → the transport layer (the TCP) applications → layer (HTTP server)

The TCP (Transmission Control Protocol Transmission Control Protocol ) is a connection-oriented, reliable transport layer protocol based on a stream of bytes .

Well, now we return to the topic, specific process TCP three-way handshake is how it

Since there is only TCP request and response, both request and response packets. To better understand the process of TCP three-way handshake, we need to understand TCP packet
Here Insert Picture Description
of the more important fields are:

Number (sequence number): Seq ID, accounting for 32, used to identify the byte stream from the TCP transmission source to the destination, the initiator sends data of this labeling.

Acknowledgment number (acknowledgement number): Ack number, accounting for 32, when only the ACK flag bit is 1, the acknowledgment number field is valid, Ack = Seq + 1.

Flag (Flags): a total of six, i.e. URG, ACK, PSH, RST, SYN, FIN like.

Six flags specific meanings are as follows:

URG:紧急指针(urgent pointer)有效。
ACK:确认序号有效。
PSH:接收方应该尽快将这个报文发送给应用层。
RST:重置连接。
SYN:发起一个新连接。
FIN:释放一个连接。

注意:不要将确认序号 Ack 与标志位中的 ACK 搞混了。

确认方 Ack=发起方 Seq+1,两端配对。

了解了TCP报文首部了现在我们来看具体的过程:
Here Insert Picture Description
初始化状态:客户端:close关闭状态,服务器:listen监听状态。

第一次握手:客户端发送请求报文将 SYN=1 同步序列号和初始化 seq=x发送给服务端,服务端从初始化状态,创建连接,等待客户端,确认接收后的状态为SYN_Receive。这个时候客户端处于等待状态为SYN_Send。

第二次握手,服务器接收到报文后(SYN=1,seq=x)收到请求后请求报文变为同步序列号SYN=1,初始化序列号seq=1,确认号ACK=1,ack=x+1,服务器为SYN_Receive状态,发送端的状态为:SYN_Send。

第三次握手,客户端收到服务端的数据包(收到响应后),然后发送同步序列号ack=y+1和数据包的序列号seq=x+1和ACK=1确认包作为应答(第三次握手:ACK=1,seq=x+1,ack=y+1),客户端和服务端变化为established状态。
动态过程如下图所示
Here Insert Picture Description

Here Insert Picture Description
我们把客户端和服务器比作两名网瘾少年

这个过程我们可以通俗的理解为:
Here Insert Picture Description
至此,三次握手完成,TCP客户端和服务器端成功地建立连接,客户端可以访问网络并开始快乐了
Here Insert Picture Description
但是,先别忙着快乐,我们还有一点没整明白:为什么要进行第三次握手?

A: In order to prevent the server is enabled useless server overhead and increase the connection has failed to prevent the connection request segment suddenly transferred to the server, and thus a wrong connection .

Since the network is a delay (through the optical fiber network and the various intermediate proxy server), in the process of transmission, such as the client initiated SYN = 1 to create a connection request (first handshake).

If the server directly created the connection and return data with SYN, ACK packet content and Seq and other clients, because of the packet transmission network is lost, is lost after the client has not received the data returned by the server package.

The client may have set a timeout period, the time to shut down the connection request is created.

And then reissue the request to create a connection, and the server is not known if there is no third handshake tells the server the client received the server-side data transmission, then the server does not know the client has not received the server return information.

This process is understood as:
Here Insert Picture Description
There should be connected to the release, when the established connection without the (happy over the Internet), will take the initiative to release the connection release party, the other passively released, this is what we often say that the TCP four waved .

This reference to 51CTO blog, For a more detailed explanation, please see the following link poke
three-way handshake + four wave

Published 21 original articles · won praise 17 · views 5485

Guess you like

Origin blog.csdn.net/zhang175gl/article/details/104105589