May 2019
Intermediate to advanced
478 pages
11h 21m
English
The last argument taken by select() allows us to specify a timeout. It expects a pointer to struct timeval. The timeval structure is declared as follows:
struct timeval { long tv_sec; long tv_usec;}
tv_sec holds the number of seconds, and tv_usec holds the number of microseconds (1,000,000th second). If we want select() to wait a maximum of 1.5 seconds, we can call it like this:
struct timeval timeout;timeout.tv_sec = 1;timeout.tv_usec = 500000;select(max_socket+1, ©, 0, 0, &timeout);
In this case, select() returns after a socket in fd_set copy is ready to read or after 1.5 seconds has elapsed, whichever is sooner.
If timeout.tv_sec = 0 and timeout.tv_usec = 0, then select() returns immediately (after changing the fd_set ...
Read now
Unlock full access