使用Winsock2,下面的代码序列为select()返回-1 (失败)。
#include <Winsock2.h>
#include <stdio.h>
...
int rc;
int fdstdin = fileno(stdin); /* returns 0 as expected */
fd_set fds;
FD_ZERO(&fds);
FD_SET(fdstdin, &fds);
rc = select(1, &fds, NULL, NULL, NULL);
...这是使用Winsock2时的预期行为吗?还是我遗漏了什么?
发布于 2012-01-08 07:04:18
这是预期的行为。正如在the documentation中所提到的,winsock的select函数只在套接字上工作,而stdin不是套接字。
如果您调用了WSAGetLastError,您无疑会发现原因是
其中一个描述符集
WSAENOTSOCK包含一个不是套接字的项目。
试试WSAEventSelect和WaitForMultipleObjectsEx;后者也可以等待普通的文件句柄,以及普通文件句柄上未完成的读取操作产生的重叠事件对象。
https://stackoverflow.com/questions/8773838
复制相似问题