问题
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