我有一个问题:有时(不是定期)recv返回 -1errno == EAGAIN 使用时 epolledge-triggered 模式。代码:

server_sock = startup(&port);

if ( (epollfd = epoll_create(4096)) < 0) {
    perror("epoll_create error");
    exit(EXIT_FAILURE);
}

ev.events = EPOLLIN | EPOLLET;
ev.data.fd = server_sock;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, server_sock, &ev) == -1) {
    perror("epoll_ctl: server_sock");
    exit(EXIT_FAILURE);
}

while (1) {
    int nfds = epoll_wait(epollfd, events, 4096, -1);
    if (nfds == -1) {
        perror("epoll_wait");
        exit(EXIT_FAILURE);
    }

    for (int i = 0; i < nfds; i++) {
        if (events[i].data.fd == server_sock) {
            client_sock = accept(server_sock,
                         (struct sockaddr *)&client_name,
                         (socklen_t *)(&client_name_len));

        if (client_sock == -1) //server overloaded
            continue;

        if (events[i].events & EPOLLIN) {
            std::cout << "EPOLLIN on " << client_sock << std::endl;
        }

        Arch::set_nonblocking(client_sock);
        ev.events = EPOLLIN | EPOLLRDHUP | EPOLLET; //input data and connection closing
        ev.data.fd = client_sock;

        if (epoll_ctl(epollfd, EPOLL_CTL_ADD, client_sock, &ev) == -1) {
            perror("epoll_ctl: client_socket");
            exit(EXIT_FAILURE);
        }

        accept_request(client_sock);

        } else {
            if (events[i].events & EPOLLRDHUP) {
                epoll_ctl(epollfd, EPOLL_CTL_DEL, events[i].data.fd, &ev);
            }
        }
    }
}

startup(&port) 创建非阻止插座,与端口绑定等。我的脚本发送以下数据:GET /connect?id=1&secret=1 HTTP/1.0\r\n\r\n 但有时候 recv 返回 -1 在此功能中(在内部呼叫 accept_request) :

/**********************************************************************/
/* Get a line from a socket, whether the line ends in a newline,
 * carriage return, or a CRLF combination.  Terminates the string read
 * with a null character.  If no newline indicator is found before the
 * end of the buffer, the string is terminated with a null.  If any of
 * the above three line terminators is read, the last character of the
 * string will be a linefeed and the string will be terminated with a
 * null character.
 * Parameters: the socket descriptor
 *             the buffer to save the data in
 *             the size of the buffer
 * Returns: the number of bytes stored (excluding null) */
/**********************************************************************/
int get_line(int sock, char *buf, int size) {
    int i = 0;
    char c = '\0';
    int n;

    while ((i < size - 1) && (c != '\n')) {
        n = recv(sock, &c, 1, 0);
        //debug
        std::cout << "n = " << n << std::endl;
        if (n > 0) {
            if (c == '\r') {
                n = recv(sock, &c, 1, MSG_PEEK);
                if ((n > 0) && (c == '\n'))
                    recv(sock, &c, 1, 0);
                else
                    c = '\n';
            }
            buf[i] = c;
            i++;
        } else {
            //debug
            if (errno == EWOULDBLOCK)
                std::cout << "EWOULDBLOCK" << std::endl;
            c = '\n';
        }
    }
    buf[i] = '\0';

    return(i);
}

正如Epoll Man Page写的那样,我必须阅读/写作,直到我得到 EAGAIN, ,但是我已经知道了!而且我确定缓冲区不会空。我做错了什么?

UPD: 我发现了一个有趣的事情:当发生这种情况时,我在代码中使用 sleep(1)recc(...) 再次,我得到了我期望的数据!这是一个肮脏的把戏。还有其他优雅的方法可以解决这个问题吗?

有帮助吗?

解决方案

第一个完全正常 recv() 在这种情况下要返回 EAGAIN. epoll() 从来没有告诉过您是否可以阅读。

每一个 recv() 应该准备好处理 EAGAIN 如果您使用的是非阻滞插座。出现虚假的唤醒是可能的,因此每当API喜欢 select(), poll() 或者 epoll() 告诉您插座是可以读取的,只是说“它 可能 可读 - 尝试一下。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top