使用 select() 超时 [英] Usage of select() for timeout

查看:73
本文介绍了使用 select() 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只能将最多 20 秒设置为 select () API 中的超时参数.无论我给出的值高于 20,select() 都会在 20 秒后返回......所以我试图为这样的 1 分钟超时编写一个循环

I was only able to set maximum of 20 seconds as the timeout parameter in select () API. Whatever value i gave above 20, select() is returning after 20 seconds itself... So i was trying to write a loop for the timeout of 1 minute like this

    int timeoutcount = 0;
    do
    {
    FD_ZERO(&fd);
    FD_SET(sock,&fd);
    timeout.tv_sec = 20;
    timeout.tv_usec = 0;
    rc = select (sock+1,&fd,null,null,&timeout);
    if(rc ==0)
    timeoutcount += 20;
    }
    while(rc ==0 && timeoutcount <60)

请帮帮我……我走对了路吗?如果是这样,第一次超时后选择返回 1 ..也帮我解决这个问题注意:我在目标 C 中使用它

please help me out...am i going in the correct way? If so,select returns 1 after first timeout..help me figure this out too Note: i'm using it in objective C

推荐答案

选择超时没有 20 秒的最大值 -- 其他的东西(很可能是你的套接字上准备读取的数据)必须是导致 select() 提前返回.如果您真的只想使用 select() 作为一种睡眠方式,请尝试像这样调用它:

There is no 20-second maximum for the timeout to select -- something else (most likely data being ready-for-read on your socket) must have been causing select() to return early. If you really only want to use select() as a way to sleep, try calling it like this:

struct timeval tv = {600, 0};   // sleep for ten minutes!
if (select(0, NULL, NULL, NULL, &tv) < 0) perror("select");

这篇关于使用 select() 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆