Using select () for timeout

I could set a maximum of 20 seconds as a timeout parameter in the select () API. No matter what value I gave above 20, select () returns after 20 seconds ... So I tried to write a loop for a 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 ... am i going right? If so, select returns 1 after the first timeout .. help me figure this out too Note: I use it in the C object

+4
source share
1 answer

There is no 20 second maximum to select a timeout - something else (most likely the data ready for reading on your socket) should have called select () to return earlier. If you really 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"); 
+7
source

Source: https://habr.com/ru/post/1402582/


All Articles