C/C++ Linux setsockopt的SO_KEEPALIVE选项

本文介绍了在C/C++编程中如何在Linux上使用setsockopt设置TCP长连接的SO_KEEPALIVE选项。TCP_KEEPIDLE参数定义了多久的无活动状态会触发KEEPALIVE传输,默认为两小时。TCP_KEEPINTVL参数指定了如果KEEPALIVE传输未确认,尝试之间的间隔,默认为75秒。TCP_KEEPCNT参数设定了最大保持探测次数,其值在1到系统tcp_keepcnt参数值之间。了解这些配置对于有效地管理TCP长连接至关重要。
摘要由CSDN通过智能技术生成

TCP长连接。

设置:

  • TCP_KEEPIDLE: 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)
  • TCP_KEEPINTVL: 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)
  • TCP_KEEPCNT: 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.
void set_keepalive(int fd)
{
   
	int keep_alive = 1;
 	int keep_idle = 1000;
	int keep_interval = 10;
	int keep_count 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值