繁体   English   中英

在epoll_wait之后调用accept时,始终打印EAGAIN

[英]Always print EAGAIN when calling accept after epoll_wait

我正在使用epoll监视listen fd事件,在发生EPOLLIN事件后,我将调用accept进行处理,但始终会出现EAGAIN错误。 有人可以给我一些建议吗? 谢谢!

[log]始终打印以下信息

INFO Jan 01 00:02:08:924 [385] poll_loop:epoll有1个事件

INFO Jan 01 00:02:08:925 [385] poll_loop:事件fd 0,事件类型:1

INFO Jan 01 00:02:08:925 [385] handle_connect:fd 0,

错误1月1日00:02:08:925 [385] handle_connect:接受返回了错误11资源暂时不可用...正在重试,请听fd:0。

[码]

    ................

        listenfd = listen_sock (port, &addrlen);
        socket_nonblocking(listenfd);
        g_epollFd = epoll_create(MAX_EVENTS);  
        register_read(listenfd);

    .................

        while (!config.quit)
        {

            int fds = epoll_wait(g_epollFd, g_Events, MAX_EVENTS, -1);  

            if (fds < 0)
            {
                log_message(LOG_CRIT, "epoll wait error %d %s, continue", errno, strerror(errno));
                continue;
            }

            log_message(LOG_INFO, "epoll has %d event", fds);
            for (i = 0; i < fds; ++i)
             {

                log_message(LOG_INFO, "event fd %d, event type:%d", g_Events[i].data.fd, g_Events[i].events);
                 if ((g_Events[i].events & EPOLLERR) 
                    || (g_Events[i].events & EPOLLHUP)
                    /*|| (g_Events[i].events & POLLNVAL) compile err*/)
                {
                    log_message(LOG_INFO, "A disconnect occurs, [fd:%d]", g_Events[i].data.fd);
                    handle_disconnect(g_Events[i].data.fd);      
                    continue;
                }
                if (g_Events[i].events & EPOLLIN)
                {
                     if (listenfd == g_Events[i].data.fd)
                    {
                        handle_connect(listenfd, ptr);
                    }
                    else if (g_dnsfd == g_Events[i].data.fd)
                    {
                        dns_poll(g_dns);
                    }
                    else
                    {
                        handle_input(g_Events[i].data.fd);
                    }
                }
    ...................

    void handle_connect(int fd, struct child_s *ptr)
    {
        int connfd;
        struct conn_s *connptr = 0;
        char peer_ipaddr[IP_LENGTH]; 
        socklen_t clilen = sizeof(struct sockaddr_in);
        struct sockaddr_in cliaddr ;

        log_message(LOG_INFO, "fd %d, ");
        connfd = accept (fd, (struct sockaddr*)(&cliaddr), &clilen);

        if (connfd < 0) 
        {
            log_message (LOG_ERR, "accept returned an error %d %s ... retrying, listen fd:%d", errno, strerror (errno), fd);
            return;
        }

    ......................

#define REGISTER_EPOLL_EVENT(_fd, evt, op) \
do\
{\
    struct epoll_event epv = {0, {0}};  \
    epv.data.fd = _fd;  \
    epv.events  = evt;  /*EPOLLLT default*/\
\
    if(epoll_ctl(g_epollFd, op, _fd, &epv) < 0)  \
    {\
        /*if ((errno != EEXIST) && (errno != ENOENT))*/\
        {\
            log_message(LOG_ERR, "epool ctl set failed, fd %d, errno %d: %s", _fd, errno, strerror(errno));  \
            return ;\
        }\
    }\
    log_message(LOG_INFO, "epool ctl set sucess, fd %d", _fd);\
}while(0)


void register_read(int fd, int* evt)
{
    if (0 == evt)
    {
        REGISTER_EPOLL_EVENT(fd, EPOLLIN, EPOLL_CTL_ADD);
    }
    else if (0 == *evt)
    {
        *evt |= EPOLLIN;

        REGISTER_EPOLL_EVENT(fd, (*evt), EPOLL_CTL_ADD);
    }
    else if (!(*evt & EPOLLIN))
    {
        *evt |= EPOLLIN;
        REGISTER_EPOLL_EVENT(fd, (*evt), EPOLL_CTL_MOD);
    }
    else
    {
        log_message("event %d for fd %d is already there", *evt, fd);
    }

}

这行说明了一切:

INFO Jan 01 00:02:08:925 [385] poll_loop: event fd 0, event type:1

fd 0是STDIN,不是您的监听套接字。 显然,您是在epoll集上添加了“ 0”,而不是侦听套接字。

您没有显示在哪里调用epoll_ctl来将listenFd添加到epoll集(g_epollFd)。 大概这就是上面的register_read()的全部作用。 我希望它看起来像这样:

int result;
epoll_event readEvent = {};

readEvent.data.fd = listenFd;
readEvent.events = EPOLLIN;
result = epoll_ctl(g_epollFd, EPOLL_CTL_ADD, listenFd, &readEvent);

请注意,将listenFd指定为epoll_ctl的第三个参数,以及第四个参数(readEvent.data.fd)的成员var。

EAGAIN并不是真正的错误。

请参阅: 接受时出错:资源暂时不可用

另外,如果您有兴趣进一步阅读,我强烈建议您阅读本书,以全面了解epoll的网络编程。

http://man7.org/tlpi/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM