linux socket默认超时时间设置,linux socket用setsockopt设置了超时之后怎么取消

在网上没找到资料,索性用getsockopt把一个默认的socket的超时值读出来看看。我原来写的代码是设置接收超时,所以现在读取的时候也读的是接收超时。

// get.c

#include /* See NOTES */

#include

#include

#include

#include

int main()

{

struct timeval tv_out;

int ret = 1, sockfd, valsize;

// 初始化超时时间为一个特殊的值,确定函数正确执行

tv_out.tv_sec = 1;

tv_out.tv_usec = 2;

// 创建socket

sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (sockfd == -1)

{

printf("create socket file descriptor failed\n");

}

// 读取接收超时的值

ret = getsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv_out, &valsize);

if(ret == 0 && valsize == sizeof(tv_out))

{

printf("getsockopt success, tv_sec is %d, tv_usec is %d\n", tv_out.tv_sec, tv_out.tv_usec);

}

else

{

printf("getsockopt failed\n");

}

return 0;

}

运行输出是:

0818b9ca8b590ca3270a3433284dd417.png

原来默认是0秒0微秒,也就是超时设为0是取消超时,而不是不阻塞。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值