简体   繁体   English

将相同的套接字fd两次添加到fd_set,然后调用select

[英]Added the same socket fd twice to fd_set then calling select

I'm been reading the man page for select (from difference sources) and can't seem to get a straight explanation. 我正在阅读供选择的手册页(来自不同来源),似乎无法获得直接的解释。

Lets say I have an already connected socket like this: 可以说我有一个已经连接的套接字,如下所示:

s1 = socket(...);
connect(s1, ...)...

Now lets say I add the socket twice (eg: mistakenly) to the same fd_set like so: 现在,让我说两次将套接字添加到同一fd_set中(例如,错误地),如下所示:

fd_set readfds;


FD_ZERO(&readfds);
FD_SET(s1, &readfds);
....
FD_SET(s1, &readfds);

Now I call select: 现在我叫select:

int rv = select(n, &readfds, NULL, NULL, &tv);

if (rv == -1) {
   perror("select"); // error occurred in select()
}
else if (rv == 0) {
   printf("Timeout occurred!  No data after 10.5 seconds.\n");
}
else {
   // one the descriptors have data
   .....
}

If data is send from the socket, will select have set both FDs as ready or only the first one that I've added? 如果数据是从套接字发送的,是否会选择将两个FD都设置为就绪状态还是仅将我添加的第一个设置为?

Since FD_SET is a set (in the mathematical meaning of the word), any file descriptor is either in it or it is not . 由于FD_SET是一个集合(按单词的数学含义),因此任何文件描述符都位于其中也可能不在其中 Adding the same descriptor to the set more than once has no effect. 将同一描述符多次添加到集合中没有任何效果。

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

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