我创建了一个套接字来接收服务器数据,并采用非阻塞模式,但我很困惑为什么选择总是返回零?这会使我的播放器播放或暂停片刻,如果您需要任何进一步的信息,请告诉我。
int ret = 0;
int timeout = 0;
while(http->work_flag)
{
fd_set readSet;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 80*1000;
FD_ZERO( &readSet );
FD_SET( http->fd, &readSet );
ret = select(http->fd + 1,&readSet,0,0,&tv);
printf("%d\r\n",ret);
if ( ret > 0 ) {
ret = recv(http->fd,buf,size,0);
if( ret <= 0 ){
ret = -1;
}
else {
http->total_bytes += ret;
http->continue_pkt++;
}
return ret;
}
else if( ret < 0 ) {
http->continue_pkt = 0;
return -1;
}
else if( ret == 0 ) {
http->continue_pkt = 0;
//time out
timeout++;
if( timeout > 12*30 ) //30seconds
return -1;//timeout
}
}
return -1;