欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
linux下使用TCP存活(keepalive)定時(shí)器_Embedded Resource...

一、什么是keepalive定時(shí)器?[1]

在一個(gè)空閑的(idleTCP連接上,沒(méi)有任何的數據流,許多TCP/IP的初學(xué)者都對此感到驚奇。也就是說(shuō),如果TCP連接兩端沒(méi)有任何一個(gè)進(jìn)程在向對方發(fā)送數據,那么在這兩個(gè)TCP模塊之間沒(méi)有任何的數據交換。你可能在其它的網(wǎng)絡(luò )協(xié)議中發(fā)現有輪詢(xún)(polling),但在TCP中它不存在。言外之意就是我們只要啟動(dòng)一個(gè)客戶(hù)端進(jìn)程,同服務(wù)器建立了TCP連接,不管你離開(kāi)幾小時(shí),幾天,幾星期或是幾個(gè)月,連接依舊存在。中間的路由器可能崩潰或者重啟,電話(huà)線(xiàn)可能go down或者back up,只要連接兩端的主機沒(méi)有重啟,連接依舊保持建立。

這就可以認為不管是客戶(hù)端的還是服務(wù)器端的應用程序都沒(méi)有應用程序級(application-level)的定時(shí)器來(lái)探測連接的不活動(dòng)狀態(tài)(inactivity),從而引起任何一個(gè)應用程序的終止。然而有的時(shí)候,服務(wù)器需要知道客戶(hù)端主機是否已崩潰并且關(guān)閉,或者崩潰但重啟。許多實(shí)現提供了存活定時(shí)器來(lái)完成這個(gè)任務(wù)。

存活定時(shí)器是一個(gè)包含爭議的特征。許多人認為,即使需要這個(gè)特征,這種對對方的輪詢(xún)也應該由應用程序來(lái)完成,而不是由TCP中實(shí)現。此外,如果兩個(gè)終端系統之間的某個(gè)中間網(wǎng)絡(luò )上有連接的暫時(shí)中斷,那么存活選項(option)就能夠引起兩個(gè)進(jìn)程間一個(gè)良好連接的終止。例如,如果正好在某個(gè)中間路由器崩潰、重啟的時(shí)候發(fā)送存活探測,TCP就將會(huì )認為客戶(hù)端主機已經(jīng)崩潰,但事實(shí)并非如此。

存活(keepalive)并不是TCP規范的一部分。在Host Requirements RFC羅列有不使用它的三個(gè)理由:(1)在短暫的故障期間,它們可能引起一個(gè)良好連接(good connection)被釋放(dropped),(2)它們消費了不必要的寬帶,(3)在以數據包計費的互聯(lián)網(wǎng)上它們(額外)花費金錢(qián)。然而,在許多的實(shí)現中提供了存活定時(shí)器。

一些服務(wù)器應用程序可能代表客戶(hù)端占用資源,它們需要知道客戶(hù)端主機是否崩潰。存活定時(shí)器可以為這些應用程序提供探測服務(wù)。Telnet服務(wù)器和Rlogin服務(wù)器的許多版本都默認提供存活選項。

個(gè)人計算機用戶(hù)使用TCP/IP協(xié)議通過(guò)Telnet登錄一臺主機,這是能夠說(shuō)明需要使用存活定時(shí)器的一個(gè)常用例子。如果某個(gè)用戶(hù)在使用結束時(shí)只是關(guān)掉了電源,而沒(méi)有注銷(xiāo)(log off),那么他就留下了一個(gè)半打開(kāi)(half-open)的連接。在圖18.16,我們看到如何在一個(gè)半打開(kāi)連接上通過(guò)發(fā)送數據,得到一個(gè)復位(reset)返回,但那是在客戶(hù)端,是由客戶(hù)端發(fā)送的數據。如果客戶(hù)端消失,留給了服務(wù)器端半打開(kāi)的連接,并且服務(wù)器又在等待客戶(hù)端的數據,那么等待將永遠持續下去。存活特征的目的就是在服務(wù)器端檢測這種半打開(kāi)連接。

二、keepalive如何工作?[1]

在此描述中,我們稱(chēng)使用存活選項的那一段為服務(wù)器,另一端為客戶(hù)端。也可以在客戶(hù)端設置該選項,且沒(méi)有不允許這樣做的理由,但通常設置在服務(wù)器。如果連接兩端都需要探測對方是否消失,那么就可以在兩端同時(shí)設置(比如NFS)。

若在一個(gè)給定連接上,兩小時(shí)之內無(wú)任何活動(dòng),服務(wù)器便向客戶(hù)端發(fā)送一個(gè)探測段。(我們將在下面的例子中看到探測段的樣子。)客戶(hù)端主機必須是下列四種狀態(tài)之一:

1) 客戶(hù)端主機依舊活躍(up)運行,并且從服務(wù)器可到達。從客戶(hù)端TCP的正常響應,服務(wù)器知道對方仍然活躍。服務(wù)器的TCP為接下來(lái)的兩小時(shí)復位存活定時(shí)器,如果在這兩個(gè)小時(shí)到期之前,連接上發(fā)生應用程序的通信,則定時(shí)器重新為往下的兩小時(shí)復位,并且接著(zhù)交換數據。

2) 客戶(hù)端已經(jīng)崩潰,或者已經(jīng)關(guān)閉(down),或者正在重啟過(guò)程中。在這兩種情況下,它的TCP都不會(huì )響應。服務(wù)器沒(méi)有收到對其發(fā)出探測的響應,并且在75秒之后超時(shí)。服務(wù)器將總共發(fā)送10個(gè)這樣的探測,每個(gè)探測75秒。如果沒(méi)有收到一個(gè)響應,它就認為客戶(hù)端主機已經(jīng)關(guān)閉并終止連接。

3) 客戶(hù)端曾經(jīng)崩潰,但已經(jīng)重啟。這種情況下,服務(wù)器將會(huì )收到對其存活探測的響應,但該響應是一個(gè)復位,從而引起服務(wù)器對連接的終止。

4) 客戶(hù)端主機活躍運行,但從服務(wù)器不可到達。這與狀態(tài)2類(lèi)似,因為TCP無(wú)法區別它們兩個(gè)。它所能表明的僅是未收到對其探測的回復。

服務(wù)器不必擔心客戶(hù)端主機被關(guān)閉然后重啟的情況(這里指的是操作員執行的正常關(guān)閉,而不是主機的崩潰)。當系統被操作員關(guān)閉時(shí),所有的應用程序進(jìn)程(也就是客戶(hù)端進(jìn)程)都將被終止,客戶(hù)端TCP會(huì )在連接上發(fā)送一個(gè)FIN。收到這個(gè)FIN后,服務(wù)器TCP向服務(wù)器進(jìn)程報告一個(gè)文件結束,以允許服務(wù)器檢測這種狀態(tài)。

在第一種狀態(tài)下,服務(wù)器應用程序不知道存活探測是否發(fā)生。凡事都是由TCP層處理的,存活探測對應用程序透明,直到后面2,3,4三種狀態(tài)發(fā)生。在這三種狀態(tài)下,通過(guò)服務(wù)器的TCP,返回給服務(wù)器應用程序錯誤信息。(通常服務(wù)器向網(wǎng)絡(luò )發(fā)出一個(gè)讀請求,等待客戶(hù)端的數據。如果存活特征返回一個(gè)錯誤信息,則將該信息作為讀操作的返回值返回給服務(wù)器。)在狀態(tài)2,錯誤信息類(lèi)似于“連接超時(shí)”。狀態(tài)3則為“連接被對方復位”。第四種狀態(tài)看起來(lái)像連接超時(shí),或者根據是否收到與該連接相關(guān)的ICMP錯誤信息,而可能返回其它的錯誤信息。

三、在Linux中如何使用keepalive?[2]

Linux has built-in support for keepalive. You need to enable TCP/IP networking in order to use it. You also need procfs support and sysctl support to be able to configure the kernel parameters at runtime.

The procedures involving keepalive use three user-driven variables:

 

tcp_keepalive_time

the interval between the last data packet sent (simple ACKs are not considered data) and the first keepalive probe; after the connection is marked to need keepalive, this counter is not used any further

tcp_keepalive_intvl

the interval between subsequential keepalive probes, regardless of what the connection has exchanged in the meantime

tcp_keepalive_probes

the number of unacknowledged probes to send before considering the connection dead and notifying the application layer

 

Remember that keepalive support, even if configured in the kernel, is not the default behavior in Linux. Programs must request keepalive control for their sockets using the setsockopt interface. There are relatively few programs implementing keepalive, but you can easily add keepalive support for most of them following the instructions.

上面一段話(huà)已經(jīng)說(shuō)得很明白,linux內核包含對keepalive的支持。其中使用了三個(gè)參數:tcp_keepalive_time(開(kāi)啟keepalive的閑置時(shí)長(cháng))tcp_keepalive_intvl(keepalive探測包的發(fā)送間隔) 和tcp_keepalive_probes (如果對方不予應答,探測包的發(fā)送次數);如何配置這三個(gè)參數呢?

There are two ways to configure keepalive parameters inside the kernel via userspace commands:

 

  • procfs interface

  • sysctl interface

 

We mainly discuss how this is accomplished on the procfs interface because it's the most used, recommended and the easiest to understand. The sysctl interface, particularly regarding the sysctl(2) syscall and not the sysctl(8) tool, is only here for the purpose of background knowledge.

The procfs interface

This interface requires both sysctl and procfs to be built into the kernel, and procfs mounted somewhere in the filesystem (usually on /proc, as in the examples below). You can read the values for the actual parameters by "catting" files in /proc/sys/net/ipv4/ directory:

 

  # cat /proc/sys/net/ipv4/tcp_keepalive_time            7200            # cat /proc/sys/net/ipv4/tcp_keepalive_intvl            75            # cat /proc/sys/net/ipv4/tcp_keepalive_probes            9

 

 

The first two parameters are expressed in seconds, and the last is the pure number. This means that the keepalive routines wait for two hours (7200 secs) before sending the first keepalive probe, and then resend it every 75 seconds. If no ACK response is received for nine consecutive times, the connection is marked as broken.

Modifying this value is straightforward: you need to write new values into the files. Suppose you decide to configure the host so that keepalive starts after ten minutes of channel inactivity, and then send probes in intervals of one minute. Because of the high instability of our network trunk and the low value of the interval, suppose you also want to increase the number of probes to 20.

Here's how we would change the settings:

 

  # echo 600 > /proc/sys/net/ipv4/tcp_keepalive_time            # echo 60 > /proc/sys/net/ipv4/tcp_keepalive_intvl            # echo 20 > /proc/sys/net/ipv4/tcp_keepalive_probes

 

 

To be sure that all succeeds, recheck the files and confirm these new values are showing in place of the old ones.

這樣,上面的三個(gè)參數配置完畢。使這些參數重啟時(shí)保持不變的方法請閱讀參考文獻[2]。

四、在程序中如何使用keepalive?[2]-[4]

All you need to enable keepalive for a specific socket is to set the specific socket option on the socket itself. The prototype of the function is as follows:

int setsockopt(int s, int level, int optname,            const void *optval, socklen_t optlen)

The first parameter is the socket, previously created with the socket(2); the second one must be SOL_SOCKET, and the third must be SO_KEEPALIVE . The fourth parameter must be a boolean integer value, indicating that we want to enable the option, while the last is the size of the value passed before.

According to the manpage, 0 is returned upon success, and -1 is returned on error (and errno is properly set).

There are also three other socket options you can set for keepalive when you write your application. They all use the SOL_TCP level instead of SOL_SOCKET, and they override system-wide variables only for the current socket. If you read without writing first, the current system-wide parameters will be returned.

  • TCP_KEEPCNT: overrides tcp_keepalive_probes

  • TCP_KEEPIDLE: overrides tcp_keepalive_time

  • TCP_KEEPINTVL: overrides tcp_keepalive_intvlint keepAlive = 1; // 開(kāi)啟keepalive屬性

    我們看到keepalive是一個(gè)開(kāi)關(guān)選項,可以通過(guò)函數來(lái)使能。具體地說(shuō),可以使用以下代碼:

    setsockopt(rs, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive, sizeof(keepAlive));

    上面英文資料中提到的第二個(gè)參數可以取為SOL_TCP,以設置keepalive的三個(gè)參數(具體代碼參考文獻[3]),在程序中實(shí)現需要頭文件“netinet/tcp.h”。當然,在實(shí)際編程時(shí)也可以采用系統調用的方式配置的keepalive參數。

    關(guān)于setsockopt的其他參數可以參考文獻[4]。

    五、如何判斷TCP連接是否斷開(kāi)?[3]

    當tcp檢測到對端socket不再可用時(shí)(不能發(fā)出探測包,或探測包沒(méi)有收到ACK的響應包),select會(huì )返回socket可讀,并且在recv時(shí)返回-1,同時(shí)置上errno為ETIMEDOUT。

  • 本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
    打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
    猜你喜歡
    類(lèi)似文章
    修改Linux內核參數提高服務(wù)器并發(fā)能力
    Linux內核 TCP/IP、Socket參數調優(yōu)
    在Linux中打開(kāi)了太多文件(Too many open files)的解決方法
    【服務(wù)器優(yōu)化】Linux內核tcp調優(yōu)方案,sysctl.conf的設置。解釋各參數含義
    proc/sys/net/ipv4/下各項的意義
    生活服務(wù)
    分享 收藏 導長(cháng)圖 關(guān)注 下載文章
    綁定賬號成功
    后續可登錄賬號暢享VIP特權!
    如果VIP功能使用有故障,
    可點(diǎn)擊這里聯(lián)系客服!

    聯(lián)系客服

    欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久