简体   繁体   English

在用单个FD调用`select`之后是否需要调用`FD_SET`?

[英]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? 问:从fd读取之前,是否必须在上面的代码中使用FD_ISSET 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 . 根据我的理解,读取集中只有一个fd,并且返回值大于0,那么传入的fd应该始终位于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. 如果select()的返回值与在所有输入fd_set中设置的FD的总数相同,则无需调用FD_SET 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. 仅用一个fd_set调用它的情况,它只有一个FD集,而select返回1只是这种情况的特例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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