设置TCP的keepalive来进行网络联调

使用TCP的keepalive来检查网络错误

为了检测网络错误和信令连接问题,你可以开启TCP的keep alive 功能。

它会增加信令使用的带宽,但信令通道使用的带宽要小于它的实际带宽,增加得并不多。

而且,还可以控制它keep alive的超时时长。

问题是大多数的系统对TCP keepalive的超时时长为7200秒,约两个小时。

你可能会想要这个时间更短此,如一分钟等。

对于每个系统,调整这个参数的方式是不一样的。

在设置完所有的相关参数后,需要检测下这些设置是否生效,

就需要生成一个测试调用,然后拔出网线,看这个调用是否在超时时长后结束。

一、Linux 系统

使用 sysctl -A 命令查看所有有效的内核变量,使用 grep net.ipv4 过滤。

$ sysctl -A | grep net.ipv4

应当有下列变量

- net.ipv4.tcp_keepalive_time   - 在第一次keep alive请求发送后,不活动连接的时间

- net.ipv4.tcp_keepalive_probes - 在这个连接被认为是断开之前,keep alive请求被重发的次数

- net.ipv4.tcp_keepalive_intvl  - keep alive探测的时间间隔

可以使用下面的命令操作这些变量:

$ sysctl -w net.ipv4.tcp_keepalive_time=60 net.ipv4.tcp_keepalive_probes=3 net.ipv4.tcp_keepalive_intvl=10

这个命令将TCP keepalive的超时设为60秒,并有三次重发,每次间隔10秒。

因此,你的应用程序90秒(60  + 10 + 10 + 10)后检测到TCP断开

二、FreeBSD and MacOS X

For the list of available TCP settings (FreeBSD 4.8 an up and 5.4):

sysctl -A | grep net.inet.tcp

net.inet.tcp.keepidle - Amount of time, in milliseconds, that the (TCP)

connection must be idle
before keepalive probes (if enabled) are sent.

net.inet.tcp.keepintvl - The interval, in milliseconds, between keepalive
probes sent to remote machines.

After TCPTV_KEEPCNT (default 8)
probes are sent, with no response,

the (TCP)connection is dropped.

net.inet.tcp.always_keepalive - Assume that SO_KEEPALIVE is set on all TCP
connections,

the kernel will periodically send a packet to the remote
host to

verify the connection is still up.

therefore formula to calculate maximum TCP inactive connection time is

following:

net.inet.tcp.keepidle + (net.inet.tcp.keepintvl x 8)

the result is in milliseconds.

therefore, by setting

net.inet.tcp.keepidle = 10000

net.inet.tcp.keepintvl = 5000

net.inet.tcp.always_keepalive =1 (must be 1 always)

the system will disconnect a call when TCP connection is dead for:

10000 + (5000 x 8) = 50000 msec (50 sec)

To make system remember these settings at startup, you should add them

to /etc/sysctl.conf file

Solaris

For the list of available TCP settings:

ndd /dev/tcp \?

Keepalive related variables:

- tcp_keepalive_interval - idle timeout

Example:

ndd -set /dev/tcp tcp_keepalive_interval 60000

?

三、Windows

Search Knowledge Base for article ID 120642:

http://support.microsoft.com/kb/120642/EN-US

Basically, you need to tweak some registry entries under

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters

Linux

net.ipv4.tcp_keepalive_time = 300

net.ipv4.tcp_keepalive_probes = 10

net.ipv4.tcp_keepalive_intvl = 30

?

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

时间: 2024-08-07 13:04:32

设置TCP的keepalive来进行网络联调的相关文章

在Linux环境下使用TCP的keepalive机制

Linux内置支持keepalive机制,为了使用它,你需要使能TCP/IP网络,为了能够配置内核在运行时的参数,你还需要procfs和sysctl的支持. 这个过程涉及到keepalive使用的三个用户驱使的变量: tcp_keepalive_time:表示的是最近一次数据包(简单的不含数据的ACKs包)发送与第一次keepalive探针发送之间的时间间隔:当连接被标记为keepalive之后,这个计数器就不会再使用. tcp_keepalive_intvl:表示的是并发keepalive探针

TCP的keep-alive小结

TCP的keep-alive可以在不增加服务器处理逻辑的前提下,检测客户端连接是否中断 /proc/sys/net/ipv4/tcp_keepalive_time 开始首次KeepAlive探测前的TCP空闭时间 /proc/sys/net/ipv4/tcp_keepalive_intvl 两次KeepAlive探测间的时间间隔 /proc/sys/net/ipv4/tcp_keepalive_probes 判定断开前的KeepAlive探测次数 对 于一个已经建立的tcp连接.如果在keepa

http的keep-alive和tcp的keepalive区别

原文地址:http://blog.csdn.net/oceanperfect/article/details/51064574 1.HTTP Keep-Alive在http早期,每个http请求都要求打开一个tpc socket连接,并且使用一次之后就断开这个tcp连接.使用keep-alive可以改善这种状态,即在一次TCP连接中可以持续发送多份数据而不会断开连接.通过使用keep-alive机制,可以减少tcp连接建立次数,也意味着可以减少TIME_WAIT状态连接,以此提高性能和提高htt

TCP的keepalive和应用层的heart

从长链接说起 TCP是长链接的,也就是说连接建立后,及时数年没有通信连接仍然存在.这样做的好处是:免去了DNS解析的时间,连接建立等时间,大大加快了请求的速度,同时也有利于接受服务器的实时消息.但前提是连接可用. TCP的keepalive机制 服务器为了探测对端是否还活着,于是每隔两小时发送一个keepalive报文(携带一个字节的Data).个人觉得保活机制也就是在局域网中用用,路由器表项每个几分钟老化一次,连接早就不存在了. 保活机制的缺点 1.网关设备由于保活问题,导致其连接表满,无法新

Http长连接和Keep-Alive以及Tcp的Keepalive

Keep-Alive模式:我们知道Http协议采用“请求-应答”模式,当使用普通模式,即非Keep-Alive模式时,每个请求/应答,客户端和服务器都要新建一个连接,完成之后立即断开连接:当使用Keep-Alive模式时,Keep-Alive功能使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,Keep-Alive功能避免了建立或者重新建立连接.http1.0中默认是关闭的,需要在http头加入”Connection: Keep-Alive”,才能启用Keep-Alive:http

Linux下TCP的Keepalive相关参数

一 基本原理TCP的Keepalive可以简单理解成为keep tcp alive,用来检测TCP sockets的连接是否正常或是已经断开.Keeplived的原理很简单,当建立一个TCP连接时,发送端就会创建一些计时器,其中一些计时器就是处理keeplaive相关问题的.当keepalive的计时器计数到0时,发送端就会向对端发送一些不含数据的keepalive数据包并开启ACK标志.如果得到keepalive探测包的回复,就可以认为当前的TCP连接正常,不用担心用户层面的具体实现.事实上,

Linux下TCP的keepalive相关参数学习

一 基本原理 TCP的Keepalive可以简单理解成为keep tcp alive,用来检测TCP sockets的连接是否正常或是已经断开. Keeplived的原理很简单,当建立一个TCP连接时,发送端就会创建一些计时器,其中一些计时器就是处理keeplaive相关问题的.当keepalive的计时器计数到0时,发送端就会向对端发送一些不含数据的keepalive数据包并开启ACK标志.如果得到keepalive探测包的回复,就可以认为当前的TCP连接正常,不用担心用户层面的具体实现.事实

ubuntu 设置网卡为混杂模式 以及网络配置命令

1. ifconfig eth0 promisc 设置eth0为混杂模式. ifconfig eth0 -promisc 取消它的混杂模式 [email protected]:~$ ifconfigeth0 Link encap:Ethernet HWaddr 00:0c:29:ae:a9:7f inet addr:192.168.4.197 Bcast:192.168.4.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feae:a97f/6

设置Windows的TCP/IP属性和内部网络号码

这里,以Windows XP和Windows 7版本为例. 在安装了IPX/SPX协议或TCP/IP协议的Windows计算机上可以设置计算机的内部网络号码,主要可以防止进行局域网连接时出现冲突现象. 说明:在Windows 7.8.10中,是已经没有了IPX/SPX协议了. 步骤一:选择"属性" 步骤二:选择"本地连接",再"属性" 步骤三:选择"Internet 协议(TCP/IP)",再"属性".若是