69,371
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <iostream>
#include “winsock2.h”
#pragma comment(lib, "ws2_32.lib")
int main(void)
{
char buf[10] = "";
fd_set rdfds;
struct timeval tv;
int ret = 0;
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
FD_ZERO(&rdfds);
FD_SET(0, &rdfds);
tv.tv_sec = 3;
tv.tv_usec = 500;
//A successful WSAStartup call must occur before using this function.
ret = select(1, &rdfds, NULL, NULL, &tv);
if (SOCKET_ERROR == ret)
{// error occurred
std::cout << "Error Code = "<< WSAGetLastError() << std::endl;
}
else if (0 == ret)
{// the time limit expired
}
else //success
{//ret = the total number of socket handles that are ready and contained in the fd_set structures
}
return 0;
}
运行结果:
Error Code = 10038 //An operation was attempted on something that is not a socket.
说明windows下的select函数只能操作socket的文件描述符