tcp 連接的11種狀態

http://liujianguangaaa.iteye.com/blog/975445

通常情況下:一個正常的TCP連接,都會有三個階段:1TCP三次握手;2、數據傳送;3TCP四次揮手

 

:以下說明最好能結合:TCP的狀態機來理解。

 

SYN:(同步序列編號,Synchronize Sequence Numbers)該標誌僅在三次握手建立TCP連接時有效。表示一個新的TCP連接請求。

 

ACK:(確認編號,Acknowledgement Number)是對TCP請求的確認標誌,同時提示對端系統已經成功接收所有數據。

 

FIN:(結束標誌,FINish)用來結束一個TCP回話.但對應端口仍處於開放狀態,準備接收後續數據。

 

1)LISTEN:首先服務端需要打開一個socket進行監聽,狀態爲LISTEN. 

2)SYN_SENT:客戶端通過應用程序調用connect進行active open.於是客戶端tcp發送一個SYN以請求建立一個連接.之後狀態置爲SYN_SENT. 

 

3)SYN_RECV:服務端應發出ACK確認客戶端的SYN,同時自己向客戶端發送一個SYN. 之後狀態置爲SYN_RECV  

 

4)ESTABLISHED: 代表一個打開的連接,雙方可以進行或已經在數據交互了。

 

5)FIN_WAIT1:主動關閉(active close)端應用程序調用close,於是其TCP發出FIN請求主動關閉連接,之後進入FIN_WAIT1狀態.

6)
CLOSE_WAIT:被動關閉(passive close)TCP接到FIN後,就發出ACK以迴應FIN請求(它的接收也作爲文件結束符傳遞給上層應用程序),並進入CLOSE_WAIT. 


7)
FIN_WAIT2:主動關閉端接到ACK後,就進入了FIN-WAIT-2 .


8) 
LAST_ACK:被動關閉端一段時間後,接收到文件結束符的應用程序將調用CLOSE關閉連接。這導致它的TCP也發送一個 FIN,等待對方的ACK.就進入了LAST-ACK . 

9)
TIME_WAIT:在主動關閉端接收到FIN後,TCP就發送ACK包,並進入TIME-WAIT狀態。

 

10)CLOSING: 比較少見.


11)
CLOSED: 被動關閉端在接受到ACK包後,就進入了closed的狀態。連接結束.

TIME_WAIT狀態的形成只發生在主動關閉連接的一方。

主動關閉方在接收到被動關閉方的FIN請求後,發送成功給對方一個ACK,將自己的狀態由FIN_WAIT2修改爲TIME_WAIT,而必須再等2倍的MSL(Maximum Segment Lifetime,MSL是一個數據報在internetwork中能存在的時間)時間之後雙方纔能把狀態 都改爲CLOSED以關閉連接。目前RHEL裏保持TIME_WAIT狀態的時間爲60秒。

 

當然上述很多TCP狀態在系統裏都有對應的解釋或設置,可見man tcp

 

二、關於長連接和短連接:

通俗點講:短連接就是一次TCP請求得到結果後,連接馬上結束.而長連接並不馬上斷開,而一直保持着,直到長連接TIMEOUT(具體程序都有相關參數說明).長連接可以避免不斷的進行TCP三次握手和四次揮手.

長連接(keepalive)是需要靠雙方不斷的發送探測包來維持的,keepalive期間服務端和客戶端的TCP連接狀態是ESTABLISHED.目前http 1.1版本里默認都是keepalive(1.0版本默認是不keepalive)ie6/7/8firefox都默認用的是http 1.1版本了(如何查看當前瀏覽器用的是哪個版本,這裏不再贅述)Apache,java

 

一個應用至於到底是該使用短連接還是長連接,應該視具體情況而定。一般的應用應該使用長連接。

 

1Linux的相關keepalive參數

a tcp_keepalive_time – INTEGER
How often TCP sends out keepalive messages when keepalive is enabled.
Default: 2hours.

 

b tcp_keepalive_probes – INTEGER
How many keepalive probes TCP sends out, until it decides that the
connection is broken. Default value: 9.

 

c tcp_keepalive_intvl – INTEGER
How frequently the probes are send out. Multiplied by
tcp_keepalive_probes it is time to kill not responding connection,
after probes started. Default value: 75sec i.e. connection
will be aborted after ~11 minutes of retries.

2F5負載均衡上的相關參數說明

 

aKeep Alive Interval

Specifies, when enabled, how frequently the system sends data over an idle TCP connection, to determine whether the connection is still valid.

Specify: Specifies the interval at which the system sends data over an idle connection, to determine whether the connection is still valid. The default is 1800 milliseconds.

 

bTime Wait

Specifies the length of time that a TCP connection remains in the TIME-WAIT state before entering the CLOSED state.

Specify: Specifies the number of milliseconds that a TCP connection can remain in the TIME-WAIT state. The default is 2000.

cIdle Timeout

Specifies the length of time that a connection is idle (has no traffic) before the connection is eligible for deletion.

Specify: Specifies a number of seconds that the TCP connection can remain idle before the system deletes it. The default is 300 seconds.

 

3、Apache的相關參數說明

以下是Apache/2.0.61版本的默認參數和說明

aKeepAlive:

default On.Whether or not to allow persistent connections (more than

one request per connection). Set to “Off” to deactivate.

 

bMaxKeepAliveRequests:

default 100.The maximum number of requests to allow

during a persistent connection. Set to 0 to allow an unlimited amount.

We recommend you leave this number high, for maximum performance.

 

cKeepAliveTimeout:

default 15. Number of seconds to wait for the next request from the

same client on the same connection.

4、JAVA1.6的相關參數說明:

ahttp.keepAlive=
default: true

Indicates if keep alive (persistent) connections should be supported.

bhttp.maxConnections=
default: 5

Indicates the maximum number of connections per destination to be kept alive at any given

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章