星期二, 8月 06, 2013

[轉載] TCP termination 解釋

TCP TIME_WAIT的釋義
Ref:

由於TPC是全雙工傳輸, 換言之, 雙向的傳輸必須單獨進行關閉, 原則上主動請求關閉的一方A, 藉由發送FIN來請求終止這個方向的連接. 被動關閉的一方B, 收到FIN表示A-->B的方向已無資料進行傳送, 此時B-->A仍是可以進行資料傳送, B執行被動關閉.
簡言之, A欲進行關閉傳輸時, 必須通知B並確認, B欲進行關閉傳輸時, 也必須通知A並確認.
關閉連線:(如上圖所示)

(1)
當處於ESTABLISHED狀態時, TCP B欲主動關閉連線, 發送FINTCP A, 進入到FIN-WAIT-1狀態, 等待TCP A回應ACK, 表示等待確認TCP A得知TCP B要關閉連線

(2)
TCP A收到TCP BFIN, 立即回應ACKTCP B, 進入到CLOSE-WAIT狀態, 表示須等到應用程序沒有任何資料要傳送給TCP B, TCP A才決定關閉連線

(3) TCP B
收到TCP A回應的ACK, 表示確認TCP A得知TCP B要關閉連線, 此刻等待TCP A發送FIN

(4) TCP A
決定關閉連線, 發送FINTCP B, 進入到LAST-ACK狀態, 等待TCP B回應ACK, 表示等待確認TCP B得知TCP A要關閉連線

(5) TCP B
收到TCP AFIN, 隨即回應ACK, 進入TIME_WAIT狀態, 表示TCP B得知TCP A要關閉連線, 且等待2MSL時間, 以防TCP A再次發送FIN

(6) TCP A
收到TCP B回應的ACK, 進入CLOSED狀態, 表示TCP A已確認TCP B已得知它要關閉連線, 才進行關閉連線

(7) TCP B
等待2MSL時間, 才進入CLOSED狀態, 關閉連線, 並自連線表中移除

在上述的第(5),(7), 此刻TIME-WAIT的用意在於, TCP B已確認TCP A要關閉連線, 且回應了ACKTCP A, 但不保證TCP A會收到ACK, 一旦ACK遺漏, TCP A會再次發送FINTCP B, 再次進行確認, 所以TCP B須進入TIME-WAIT狀態, 等待2MSL時間, 預防TCP A會再次發送FIN, 進行連線關閉的確認


different states of a TCP connection:
LISTEN : awaiting a connection request from client (
監聽客戶端的連線請求)
SYN-SENT : a SYN has been sent to server, and client is awaiting the ACK of SYN (
發送SYN, 並等待服務端回應SYNACK)
SYN-RECEIVED : a SYN has been received from client, a SYN with ACK has been sent to client, and server is awaiting the ACK of SYN (
接收客戶端的SYN, 另傳送SYN+ACK(SYN)給客戶端, 並等待客戶端回應SYNACK)
ESTABLISHED : the three-way handshake has been completed, and established the connection (
完成握手協定, 並建立連線)
FIN-WAIT-1 : the local AP has issued a close. active TCP has sent a FIN to passive TCP, and is awaiting an ACK of FIN (
主動端發出FIN至被動端, 並等待被動端回應FINACK)
FIN-WAIT-2 : a previous FIN has been sent to passiveTCP, and received an ACK of FIN from passive TCP. active TCP is awaiting a FIN from the passive TCP (
成功接收到先前傳送至被動端FINACK, 此刻等待被動端傳送FIN)
CLOSE-WAIT : passive TCP has received a FIN from active TCP, and has sent an ACK of FIN to active TCP. passive TCP is awaiting a close request from remote AP before sending a FIN (
被動端已接收主動端的FIN, 並傳送FINACK至主動端, 此刻等待AP要求關閉連線)
LAST-ACK : previously a FIN has been received from active TCP, and an ACK of FIN has been sent to active TCP, and a FIN has been sent to active TCP. passive TCP is awaiting an ACK of FIN (
先前收到主動端的FIN, 且傳送FINACK給主動端, 另被動端傳送FIN給主動端, 此刻被動端等待主動端回應FINACK)
TIME-WAIT : FINs have been received and ACK has been sent passive TCP. active TCP is waiting 2MSLs to remove the connection from the connection table (
收到被動端的FIN, 並傳送ACK至被動端, 此刻主動端會等待2MSL的時間才將連線關閉)
CLOSED : a connection has been removed from the connection table (
連線不存在於連線表)

沒有留言:

LinkWithin-相關文件

Related Posts Plugin for WordPress, Blogger...