Usage of select() for timeout

房东的猫 提交于 2021-02-07 14:16:07

问题


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)

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


回答1:


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");


来源:https://stackoverflow.com/questions/9798948/usage-of-select-for-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!