我正在尝试进行非阻塞读取,但该函数永远不会返回。有人可以提出一些建议吗?这是我设置非阻塞 fd 的代码。
from_ap = open(FFS_GBEMU_OUT, O_RDWR|O_NONBLOCK);
if (from_ap < 0)
return from_ap;
我也尝试过类似的结果
from_ap = open(FFS_GBEMU_OUT, O_RDWR);
int status = fcntl(from_ap, F_SETFL, fcntl(from_ap, F_GETFL, 0) | O_NONBLOCK);
if (status == -1){
perror("calling fcntl");
这是我调用读取函数的地方:
rsize = read(from_ap, cport_rbuf, ES1_MSG_SIZE);
if (rsize < 0) {
printf("error %zd receiving from AP\n", rsize);
return NULL;
}
我也尝试过类似的结果:
fd_set readset;
struct timeval tv;
FD_ZERO(&readset);
FD_SET(from_ap, &readset);
tv.tv_sec = 0;
tv.tv_usec = 100;
result = select(from_ap+1, &readset, NULL, NULL, &tv);
if (result > 0 && FD_ISSET(from_ap, &readset)){
printf("there was something to read\n");
rsize=read(from_ap,cport_rbuf,ES1_MSG_SIZE);
}
收到的最后一条消息是“有东西要读”,代码没有进一步进展。我做错了什么?这不是一个多线程程序,因此没有人可以更改标志,但无论如何我已经通过在阅读之前打印回标志来确认它们。