本文介绍了Winsock2的的选择()对FD 0(标准输入)失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Winsock2的下面返回code序列 1 的(失败)选择()

Using Winsock2 the code sequence below returns -1 (failure) for select().

#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的还是我失去了一些东西时,这是预期的行为?

Is this the expected behaviour when using Winsock2 or am I missing something?

推荐答案

这是预期的行为。正如前面提到的各地,winsock的的功能仅适用于插座和标准输入不是一个套接字。

This is expected behavior. As mentioned all over the documentation, winsock's select function only works on sockets, and stdin is not a socket.

如果你有一个名为于WSAGetLastError ,你肯定会发现,原因是

If you had called WSAGetLastError, you undoubtedly would have found that the cause was

WSAENOTSOCK 一个描述符集包含一个项,它不是一个套接字。

尝试 WSAEventSelect WaitForMultipleObjectsEx ;后者也可以等待正常的文件句柄,以及从优秀的读取操作上正常的文件处理重叠的事件对象。

Try WSAEventSelect and WaitForMultipleObjectsEx; the latter can also wait on normal file handles as well as OVERLAPPED event objects from outstanding read operations on normal file handles.

这篇关于Winsock2的的选择()对FD 0(标准输入)失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 05:18