1. TCP State Transition Diagram
前面我们学习了TCP的三次握手和四次挥手,现在讲两者结合起来描绘出 TCP 协议运行时各种状态的转换关系。There are 11 different states defined for a connection.
首先我们要明白:服务器和客户端在打开连接时,两者的状态转换关系是不相同的(调用的函数也不相同);但是在关闭连接时都是调用close函数,服务器和客户端的状态变化是相同的;
2. 打开连接时的状态转换:
1)客户端状态的转换关系:
首先是开始时都是 CLOSED 状态,当调用了socket, connect 函数之后会发送 SYN J,从而进入 SYN_SENT 状态;(注意这里忽略“客户端和服务端同时打开连接”的情况)如果没有收到 ACK J+1 就会返回“connection time out”或者“connection refused";或者收到 ICMP “NO router”,从而connect 调用失败,回到 CLOSED 状态。
如果客户端收到 ACK J+1 和 SYN K,会发送 ACK K+1 从而进入 ESTABLISHED 状态;
2)服务器端状态的转换关系:
首先是开始时都是 CLOSED 状态,当调用了socket, bind, listen 函数之后,进入了 LISTEN 状态;收到 客户端的 SYN J 之后会发送 SYN J+1 和 SYN K 进入 SYN_RCVD 状态;然后会收到 ACK K+1 从而进入 ESTABLISHED 状态;然后就和客户端交互业务数据;
3. 关闭连接时的状态转换:
关闭连接时都是调用close函数,服务器先调用close得到的服务器的状态转换关系 和 客户端先调用close得到的客户端的状态转换关系是相同的;
If an application calls close before receiving a FIN (an active close),
the transition is to the FIN_WAIT_1 state. But if an application receives a FIN
while in the ESTABLISHED state (a passive close), the transition is to the
CLOSE_WAIT state.
1> 主动关闭:调用 close 函数,发送 FIN M,进入 FIN_WAIT_1 状态;然后收到 ACK M+1 进入 FIN_WAIT_2 状态(当然 ACK M+1 和 FIN N可能会同一个数据报达到,那么就没有FIN_WAIT_2 状态,直接进入TIME_WAIT 状态);然后会收到 FIN N, 之后发送 ACK N+1,进入 TIME_WAIT 状态;等待 timeout,最后回到 CLOSED 状态;
2> 被动关闭:收到 FIN M, 然后发送 ACK M+1之后进入 CLOSE_WAIT,完成剩余工作,调用close发送FIN N,进入LAST_ACK状态,收到 ACK N+1 回到 CLOSED状态;
3> 同时关闭:调用 close 函数,发送 FIN M,进入 FIN_WAIT_1 状态;与此同时会收到 FIN N ,然后发送 ACK N+1,进入 CLOSING 状态;收到 ACK M+1 之后 进入 TIME_WAIT 状态;等待 timeout;最后回到 CLOSED 状态;(同时关闭的情况下:客户端和服务器端的状态变化相同)
注意:主动关闭的一方最多有三个“WAIT”状态:FIN_WAIT_1, FIN_WAIT_2, TIME_WAIT;
而被动关闭却是 CLOSE_WAIT 状态
(其实只要明白了TCP的三次握手和四次挥手的过程,这些状态转换关系自然就能推出来。)
One reason for showing the state transition diagram is to show the 11 TCP states with their names. These states are displayed by netstat, which is a useful tool when debugging client/server applications.
4. Notice that the acknowledgment of the client's request is sent with the server's reply. This is called piggybacking and will normally happen when the time it takes the server to process the request and generate the reply is less than around 200 ms.
Another important feature provided by TCP is congestion control.(拥塞控制)
5. TIME_WAIT State
one of the most misunderstood aspects of TCP with regard to network programming is its TIME_WAIT state. the end that performs the active close goes through this state. The duration that this endpoint remains in this state is twice the maximum segment lifetime (MSL), sometimes called 2MSL.
The MSL is the maximum amount of time that any given IP datagram can live in a network.
Every implementation of TCP must choose a value for the MSL. The recommended value in RFC1122 is 2 minutes, although Berkeley-derived implementations have traditionally used a value of 30 seconds instead. This means the duration of the TIME_WAIT state is between 1 and 4 minutes.
the assumption is made that a packet with the maximum hop limit of 255 cannot exist in a network for more than MSL seconds. 我们假设:具有最大跳限(255)的分组在网络中存在的时间不可能超过MSL秒。
There are two reasons for the TIME_WAIT state:
1. To implement TCP's full-duplex connection termination reliably (可靠地实现TCP全双工连接的终止)
2. To allow old duplicate segments to expire in the network (允许老的重复分节在网络中消逝)
The first reason can be explained assuming that the final ACK is lost. The server will resend its final FIN, so the client must maintain state information, allowing it to resend the final ACK. If it did not maintain this information, it would respond with an RST, which would be interpreted by the server as an error.
So the end that performs the active close is the end that remains in the TIME_WAIT state: because that end is the one that might have to retransmit the final ACK.
To understand the second reason for the TIME_WAIT state, assume we have a TCP connection between 12.106.32.254 port 1500 and 206.168.112.219 port 21. This connection is closed and then sometime later, we establish another connection between the same IP addresses and ports: 12.106.32.254 port 1500 and 206.168.112.219 port 21. This latter connection is called an incarnation of the previous connection since the IP addresses and ports are the same. TCP must prevent old duplicates from a connection from reappearing at some later time and being misinterpreted as belonging to a new incarnation of the same connection. To do this, TCP will not initiate a new incarnation of a connection that is currently in the TIME_WAIT state. Since the duration of the TIME_WAIT state is twice the MSL, this allows MSL seconds for a packet in one direction to be lost, and another MSL seconds for the reply to be lost. By enforcing this rule, we are guaranteed that when we successfully establish a TCP connection, all old duplicates from previous incarnations of the connection have expired in the network. (incarnation [ˌinkɑ:'neiʃən] n. 赋与肉体, 具人形, 化身)
阅读(1003) | 评论(0) | 转发(0) |