简体   繁体   中英

Is it necessary to call `FD_SET` after the calling of `select` with a single FD?

//int fd <= socket fd
timeval tv;
tv.tv_sec  = 100;
tv.tv_usec = 0;

fd_set readfds;
FD_ZERO( &readfds );

FD_SET( fd, &readfds );
const int iRes = select( fd + 1, &readfds, NULL, NULL, &tv );

if (iRes > 0)
{
    if (FD_ISSET( fd, &readfds )
    {
        // read from fd            
    }        
} else {
    // 0: timeout
    // -1: error in select
}

Question: Do I have to use FD_ISSET in above code before I can read from fd? Based on my understanding, there is ONLY one fd in the read set and the return value is large than 0 then the passed in fd should be always in the readfds .

You don't need to call FD_SET if the return value of select() is the same as the total number of FD's that were set in all the input fd_set s. The case where you call it with just one fd_set , it only has one FD set, and select returns 1 is just a special case of this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM