TCP套接口利用TCP自帶的 SO_KEEPALIVE選項實現指定時間檢測是否激活狀態的方法

轉自 http://blog.chinaunix.net/uid-26575352-id-3483808.html

如有侵權,請您及時告知,我將第一時間處理。


SO_KEEPALIVE 保持連接檢測對方主機是否崩潰


SO_KEEPALIVE

在《UNIX網絡編程第1卷》中也有詳細的闡述:

SO_KEEPALIVE 保持連接檢測對方主機是否崩潰,避免(服務器)永遠阻塞於TCP連接的輸入。設置該選項後,如果2小時內在此套接口的任一方向都沒有數據交換,TCP就自 動給對方 發一個保持存活探測分節(keepalive probe)。這是一個對方必須響應的TCP分節.它會導致以下三種情況:對方接收一切正常:以期望的ACK響應。2小時後,TCP將發出另一個探測分 節。對方已崩潰且已重新啓動:以RST響應。套接口的待處理錯誤被置爲ECONNRESET,套接 口本身則被關閉。對方無任何響應:源自berkeley的TCP發送另外8個探測分節,相隔75秒一個,試圖得到一個響應。在發出第一個探測分節11分鐘 15秒後若仍無響應就放棄。套接口的待處理錯誤被置爲ETIMEOUT,套接口本身則被關閉。如ICMP錯誤是“host unreachable(主機不可達)”,說明對方主機並沒有崩潰,但是不可達,這種情況下待處理錯誤被置爲 EHOSTUNREACH。

根據上面的介紹我們可以知道對端以一種非優雅的方式斷開連接的時候,我們可以設置SO_KEEPALIVE屬性使得我們在2小時以後發現對方的TCP連接是否依然存在。   

keepAlive = 1;

Setsockopt(listenfd, SOL_SOCKET, SO_KEEPALIVE, (void*)&keepAlive, sizeof(keepAlive));


如果我們不能接受如此之長的等待時間,從TCP-Keepalive-HOWTO上可以知道一共有兩種方式可以設置,一種是修改內核關於網絡方面的 配置參數,另外一種就是SOL_TCP字段的TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT三個選項。

1) The tcp_keepidle parameter specifies the interval of inactivity that causes TCP to generate a KEEPALIVE transmission for an application that requests them. tcp_keepidle defaults to 14400 (two hours).

/*開始首次KeepAlive探測前的TCP空閉時間 */

 

2) The tcp_keepintvl parameter specifies the interval between the nine retries that are attempted if a KEEPALIVE transmission is not acknowledged. tcp_keepintvl defaults to 150 (75 seconds).
    /* 兩次KeepAlive探測間的時間間隔  */


    3) The tcp_keepcnt option specifies the maximum number of keepalive probes to be sent. The value of TCP_KEEPCNT is an integer value between 1 and n, where n is the value of the systemwide tcp_keepcnt parameter.

/* 判定斷開前的KeepAlive探測次數

    

int                 keepIdle = 1000;

int                 keepInterval = 10;

int                 keepCount = 10;

Setsockopt(listenfd, SOL_TCP, TCP_KEEPIDLE, (void *)&keepIdle, sizeof(keepIdle));

Setsockopt(listenfd, SOL_TCP,TCP_KEEPINTVL, (void *)&keepInterval, sizeof(keepInterval));

Setsockopt(listenfd,SOL_TCP, TCP_KEEPCNT, (void *)&keepCount, sizeof(keepCount));


Remember that keepalive is not program?related, but socket?related, so if you have multiple sockets, you can handle keepalive for each of them separately.

發佈了35 篇原創文章 · 獲贊 7 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章