日期:2014-05-16 浏览次数:20981 次
//设置非阻塞模式
    int flags = fcntl(sublistenfd, F_GETFL, 0); 
    fcntl(sublistenfd, F_SETFL, flags | O_NONBLOCK);
    
    fd_set rset,allset;
    FD_ZERO(&rset);
    FD_ZERO(&allset);
    FD_SET(sublistenfd,&allset);
    int nready = 0;
    int maxfd = sublistenfd +1;
    //超时设置
    struct timeval timeout,resettime;
    timeout.tv_sec = g_nTimeout;//TIMEOUT;
    timeout.tv_usec = 0;
    int nCliLen = 0;
    for ( ; ; ) 
    {
        rset = allset;
        resettime = timeout;
        nready = select(maxfd, &rset, NULL, NULL, &resettime);
        if(nready <= 0) // 超时或者出错
        {
            return;
        }
        nCliLen = sizeof(cliaddr);    
        syslog( LOG_WARNING,"accept");
        if( (subconnfd = accept(sublistenfd,(struct sockaddr*)&cliaddr,&clilen)) < 0) //如果此时客户端发送过来空串,将阻塞
        {
                syslog( LOG_WARNING,"accepted");
            if(errno == EINTR || errno==EWOULDBLOCK )  //被中断的系统调用
            {
              continue;
            }
#ifdef    EPROTO        //accept返回前连接夭折,即在客户端与服务器端建立三次握手完成后,accept返回前,客户端发送RST信号
            if (errno == EPROTO || errno == ECONNABORTED)
#else
            if (errno == ECONNABORTED)
#endif
            {
                continue;
            }
            else
            {
                close(sublistenfd);
                return;
            }
        
        }
    }