我是套接字编程的新手,我正在努力彻底了解它是如何工作的,但是现在我真的被困在了select().
问题是在我的代码中,在select检测到活动并且fd保持设置之后,似乎在下一次迭代FD_ISSET中将自动返回true,就像忽略select函数一样.问题似乎与这个问题相同,但我做了我在那里找到的所有内容并且无济于事:http://compgroups.net/comp.unix.programmer/how-does-fd_isset-return-0-after-returne/55058
select()因为我在Linux上,所以我确保重新初始化timeval变量,并且我理解这个函数在不同的操作系统上表现不同,我也在select FD_ZERO和FD_SET之前重新初始化了fd set .
我究竟做错了什么?这是代码:
#include <stdio.h>
#include <strings.h>
#include <sys/select.h>
#include <sys/time.h>
int main () {
struct timeval tv;
tv.tv_sec = 5; // 5 seconds timeout
tv.tv_usec = 0;
fd_set afds, rfds;
FD_ZERO(&afds);
FD_SET(0, &afds);
while (1) {
rfds = afds;
select(1, &rfds, NULL, NULL, &tv);
// linux, reinitialize tv?
tv.tv_sec = 5;
tv.tv_usec = 0;
// so at this point after select runs the first …Run Code Online (Sandbox Code Playgroud)