在linux和windows上使用select略有不同,linux上可以使用select(0,NULL,NULL,NULL,&tv)就能达到效果,而windows上严格些,如下:
// selectDelay.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[])
{
struct timeval tv={1,0};
WSADATA ws;
WSAStartup(MAKEWORD(2,2), &ws); /* init windows socket dll */
SOCKET delaysock = socket(AF_INET,SOCK_STREAM, 0);
fd_set s_read;
int i = 0;
while (i < 5)
{
i++;
FD_ZERO(&s_read);
FD_SET(delaysock, &s_read);
int ret = select(delaysock+1,&s_read,NULL,NULL,&tv);
printf("delay ret = %d\n",ret);
}
WSACleanup();
printf("Hello World!\n");
return 0;
}