TCP state transitions

 

       In order to get a clear grasp of all the different events that happen when the connection is established, the connection is terminated and the data is transferred, the following diagram is defined in the form of a finite state machine.

 

 Various states of TCP

Status Description

CLOSED not connected

LISTEN received passive open; waiting for SYN

SYN-SENT SYN has been sent; waiting for ACK

SYN-RCVD has sent SYN+ACK; waiting for ACK

ESTABLISHED Connection established; data transfer in progress

FIN-WAIT-1 First FIN sent; waiting for ACK

FIN-WAIT-2 ACK for a FIN has been received, waiting for a second ACK

CLOSE-WAIT first FIN received, ACK sent; waiting for application to close

TIME-WAIT 2nd FIN received; ACK sent; wait for 2MSL timeout

LAST-ACK Second FIN sent; waiting for ACK

CLOSING Both parties have decided to close at the same time

 

Connection establishment and half-close termination

       Describes the situation where the server process initiates passive opening and passive closing, and the client initiates active opening and active closing. Half-closed termination allows us to show more states.

client state

The client process issues a command to his TCP to request a connection to a specific socket address, which is called an active open. So TCP sends a SYN segment and enters the SYN-SENT state. After receiving the SYN-ACK message, TCP sends an ACK segment and enters the ESTABLISHED state. After that, the data starts to be transmitted and confirmed, generally in both directions. When the client process has no more data to transfer, a command called an active shutdown is issued. So the client TCP sends the FIN segment and enters the FIN-WAIT-1 state. When it receives the ACK of the FIN segment it just sent, it enters the FIN-WAIT-2 state and continues to stay in this state until it receives the FIN message from the server. After receiving the FIN segment, the client sends an ACK segment, enters the TIME-WAIT state, and sets a timer whose timeout is equal to the maximum segment lifetime (MSL). Twice, the MSL is the maximum time a segment can live on the Internet before it is discarded.

       We may recall that TCP segments are encapsulated in time-to-live ( TTL ) limited IP datagrams. When IP datagrams are discarded, the TCP packets encapsulated in them are also lost. A common value for MSL is 30-60 seconds. There are two reasons why we need the TIME-WAIT state and the 2MSL counter.

 

Demonstrate the same process through a timeline.

1. If the last ack segment is lost, then the serving TCP (which sets a timer for the last FIN) thinks its FIN is lost and retransmits it. If the client has entered the CLOSED state and closed the connection before the 2MSL timer expires, the client will never receive the retransmitted FIN segment, and the server will never receive the final ACK . The server could not close the connection. The 2MSL timer can make the client wait long enough to wait until the next FIN (another MSL) in the event of a lost ACK (1 MSL). If a new FIN arrives in the TIME-WAIT state, the client sends a new ACK and restarts the 2MSL timer.

2. Duplicate segments in one connection may appear in the next connection. Assume that the client and server have closed the connection. After a short time, they open a new connection using the same socket address (same source and destination address, same source and destination port numbers), such a connection is called an old connection Connected Avatars. If the time interval between the two connections is very short, it is possible that duplicate segments from the previous connection will arrive on the new connection and be interpreted as segments belonging to the new connection. To avoid this problem, TCP stipulates that such incarnations must pass 2MSL before appearing. However, in some implementations, this rule is ignored if the initial sequence number of the avatar is greater than the last sequence number used by a connection.

 

service status  

In our hypothetical case, the server process issues an open command. This must happen before the client issues the open command. The server TCP enters the Listen state and continues to passively remain in this state until the SYN message is received. When the server TCP receives the SYN message, it sends the SYN+ACK segment, and enters the SYN+RCVD state, waiting for the client to send the ACK segment. After receiving the ACK segment, it enters the ESTABLISHED state, and data transmission can be performed at this time. While both parties can initiate the shutdown process, for the most part, we assume that the client initiates the shutdown.

       TCP can remain in this state until a FIN segment is received from the client TCP, which means that there is no more data to send, so the connection can be closed. At this point, the server sends an ACK segment to the client, and sends the data that has not been sent in the queue, and then enters the CLOSE-WAIT state. Our hypothetical situation is a half-closed termination, and the service TCP can continue to send data to the client and receive acknowledgments, but no data in the opposite direction can be delivered. The server TCP can stay in this state until the application actually issues a shutdown command. This is that the server TCP sends a FIN message to the client to indicate that it has to close the link and enter the LAST-ACK state. The server TCP remains in this state until it receives the last ACK segment, and then enters the CLOSED state. From the beginning of the first FIN segment to the termination segment is called a four-way handshake [waving].

 

 

 

Common situation

       It is more common to use a three-way handshake during the connection establishment and termination phases, and the following figure shows the state transition diagram of the client and server in this case.

 

下图用时间线描述了相同的情况。其中连接建立阶段与前一种情况中的相同,我们仅展示连接终止的阶段。

 

        在数据传输完成后,客户进程发出关闭命令。客户TCP发送FIN报文,并进入FIN-WAIT-1状态。服务器TCP在收到这个FIN报文后,继续向客户端发送队列剩余的所有数据,并在最后附上EOF标记,表示这个连接要关闭了。服务器TCP进入CLOSE-WAIT状态,但它会推迟对收到的由客户发送来的FIN报文段的确认,直至它收到从自己的进程发送过来的被动关闭命令。在收到被动关闭命令后,服务器TCP就向客户发送FIN+ACK报文段,并进入LAST-ACK状态,等待最后的ACK。从中可以看出,客户取消了FIN-WAIT-2状态而直接进入TIME-WAIT状态。剩下的部分和四次挥手相同。

 

 

同时打开

       同时打开(simultaneous open)就是双方的应用程序都发出主动打开命令。这是一种非常还见的情况。此时没有客户,也没有服务器,通信的对方是对等的,彼此知道双方的本地端口号。TCP允许出现这种情况,但它很少会出现,因为双方都需要向对方发送SYN报文段,这两报文段要在同时传送。也就是说,两个应用程序必须几乎同时地发出主动打开命令,下图给出了这种情况下连接建立阶段。双方的TCP要经过SYN-SENT和SYN-RCVD状态之后才能进入ESTABLISHED状态。更仔细的观察可以看出这两个进程在同一时刻既充当了客户,又充当了服务。两个SYN-ACK报文段确认了两个SYN报文段后,连接就打开了。请注意,这个连接建立包括四次挥手。数据传送阶段和连接终止阶段和前面的例子一样。

 

 

同时关闭

      同时关闭(simultaneous close)在这种情况下,两端都主动关闭。双方的TCP都进入FIN-WAIT-1状态,并发送FIN报文段,这两个报文段同时在传送。在收到FIn报文段后,每一端都进入CLOSING状态,并发送ACK报文段。CLOSING状态取代了常见情境中FIN-WAIT-2和CLOSE-WAIT。在收到ACK后,双方都进入TIME-WAIT状态。请注意,这个时间段对双方来说都是需要的,因为双方都发送了ACK,而他们都有可能丢失。

 

 

拒绝连接

      另一种创建的情况时发生在服务TCP拒绝连接时,可能因为SYN报文段中目的端口定义的服务器当时并没有处于LISTEND状态。服务器TCP在收到这样的SYN报文段后会发送一个RST+ACK报文段,它确认了SYN报文段。但与此同时充值(拒绝)这条连接。服务器TCP进入LISTEN状态等待另一个连接。客户在收到这个RST+ACK报文段后进入CLOSED状态。

 

异常终止连接

       除了关闭连接,进程还可以异常终止连接。出现这种情况时因为进程出了故障(可以能是死锁在无限循环中),或者是不想发送队列中的数据了(由于数据中存在某些不一致)。另外,TCP也有可能想要异常终止一条连接。如果TCP收到了属于上一个连接的报文段(化身)就有可能发生这种情况。在所有这些情况下,TCP可以通过发送RST报文段使连接异常终止。图中的客户TCP发送RST+ACK报文段,并丢弃队列中的所有数据,服务TCP也把队列中的所有数据都丢弃,并通过差错报文来通知服务器进程。双方的TCP都立即进入CLOSED状态。请注意,对RST报文段不用相应ACK报文段。

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481391&siteId=291194637