我在下面有一个片段-我已经调整了我的tv_usec
在几个方面,但我一直坐在select()
循环几乎正好10秒,不管tv_usec
是什么设置。
char buffer[512];
fd_set readfds;
struct timeval tv;
tv.tv_usec = 50;
int rv = 1;
// clear the set ahead of time
FD_ZERO(&readfds);
// add our descriptors to the set
FD_SET(mySocket, &readfds);
// the n param for select()
int n = mySocket + 1;
while(rv != 0)
{
rv = select(n, &readfds, NULL, NULL, &tv);
if (rv == -1)
perror("select"); // error occurred in select()
bzero(buffer,512);
int n = recvfrom(mySocket,buffer,512,0,(struct sockaddr *)&server, &sockLen);
// do stuff...
}
tv.tv_sec
需要初始化为一些。设置tv.tv_sec = 0
解决了这个问题