我在下面有一个片段 - 我已经以多种方式调整了我的方法,但无论设置什么tv_usec
,我都会一直坐在一个循环中几乎正好 10 秒。 select()
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...
}