我創建一個套接字來接收服務器數據,並採用非阻塞模式,但我很迷惑爲什麼選擇總是返回零?這使我的球員暫停播放或暫停, 如果您需要任何進一步的信息,請讓我知道。套接字選擇始終返回零
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;
可能對您有幫助https://discussions.apple.com/message/7815634?messageID=7815634#7815634?messageID=7815634 – Sport
如果'ret'爲零,您應該返回零,以便呼叫者知道關閉FD。目前,您正在將流結束與錯誤情況混爲一談。 – EJP