【问题标题】:socket select always return zero套接字选择总是返回零
【发布时间】:2013-10-16 03:37:22
【问题描述】:

我创建了一个套接字来接收服务器数据,并采用非阻塞模式,但我很困惑为什么选择总是返回零?这让我的播放器播放或暂停片刻, 如果您需要任何进一步的信息,请告诉我。

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;

【问题讨论】:

  • 可能对您有帮助discussions.apple.com/message/…
  • 如果 'ret' 为零,则应返回零,以便调用者知道关闭 FD。目前,您将流结束与错误条件混为一谈。

标签: ios sockets select


【解决方案1】:

如果您完全摆脱循环并让select() 为您管理超时,您是否也会遇到同样的问题?

fd_set  readSet;
struct timeval tv;

tv.tv_sec = 30;
tv.tv_usec = 0;

FD_ZERO( &readSet );
FD_SET( http->fd, &readSet );

int ret = select(http->fd + 1, &readSet, 0, 0, &tv);
printf("%d\r\n", ret);

if( ret <= 0 )
{
    // error or timeout
    http->continue_pkt = 0;
    return -1;
}

ret = recv(http->fd, buf, size, 0);
if( ret <= 0 )
{
    // error or disconnect
    return -1;
}

http->total_bytes += ret;
http->continue_pkt++;   
return ret;

在具有小超时的循环中调用select() 的唯一原因是如果您试图使您的阅读逻辑尽快中止。为此,您可以通过pipe() 创建一个单独的管道并将其与您的套接字放在同一个fd_set 中,然后在管道中写入一些内容以使select() 在需要时“唤醒”。当select() 返回&gt; 0 时,代码可以检查管道是否已发出信号并退出而不调用recv()。

【讨论】:

  • 它也会发生,但如果我摆脱超时,并设置 tv.tv_usec = 800*1000;它可以正常工作。
  • 但是如果 ret
猜你喜欢
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多