歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Unix知識 >> Unix基礎知識

UNIX網絡編程:套接字選項(心跳檢測、綁定地址復用)

01./* 設置套接字選項周期性消息檢測連通性 心跳包、 心博。主要用於長連接。

02. * 參數:套接字, 1或0開啟, 首次間隔時間, 兩次間隔時間, 斷開次數

03. */

04.void setKeepAlive( int iSockfd , int iSockAttrOn, socklen_t iIdleTime , socklen_t iInterval , socklen_t iCount ){

05. setsockopt( iSockfd , SOL_SOCKET , SO_KEEPALIVE , (const char*)&iSockAttrOn , sizeof(iSockAttrOn) );

06. setsockopt( iSockfd , SOL_TCP , TCP_KEEPIDLE , (const char*)&iIdleTime , sizeof(iIdleTime) );

07. setsockopt( iSockfd , SOL_TCP , TCP_KEEPINTVL , (const char*)&iInterval,sizeof(iInterval) );

08. setsockopt( iSockfd , SOL_TCP , TCP_KEEPCNT , (const char*)&iCount,sizeof(iCount) );

09.}

10./* 綁定地址復用. 主要用於TCP Server端 */

11.setsockopt(iSockfd, SOL_SOCKET, SO_REUSEADDR, &iSockAttrOn,sizeof(iSockAttrOn) );

/* 設置套接字選項周期性消息檢測連通性 心跳包、 心博。主要用於長連接。

* 參數:套接字, 1或0開啟, 首次間隔時間, 兩次間隔時間, 斷開次數

*/

void setKeepAlive( int iSockfd , int iSockAttrOn, socklen_t iIdleTime , socklen_t iInterval , socklen_t iCount ){

setsockopt( iSockfd , SOL_SOCKET , SO_KEEPALIVE , (const char*)&iSockAttrOn , sizeof(iSockAttrOn) );

setsockopt( iSockfd , SOL_TCP , TCP_KEEPIDLE , (const char*)&iIdleTime , sizeof(iIdleTime) );

setsockopt( iSockfd , SOL_TCP , TCP_KEEPINTVL , (const char*)&iInterval,sizeof(iInterval) );

setsockopt( iSockfd , SOL_TCP , TCP_KEEPCNT , (const char*)&iCount,sizeof(iCount) );

}

/* 綁定地址復用. 主要用於TCP Server端 */

setsockopt(iSockfd, SOL_SOCKET, SO_REUSEADDR, &iSockAttrOn,sizeof(iSockAttrOn) );

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

如果我們不能接受如此之長的等待時間,從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空閉時間 */

查看本欄目更多精彩內容:http://www.bianceng.cn/OS/unix/

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探測次數

[cpp] view plaincopyprint?

01.int keepIdle = 1000;

02.int keepInterval = 10;

03.int keepCount = 10;

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

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

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

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.

Copyright © Linux教程網 All Rights Reserved