質問

私には問題があります:時々(定期的ではない)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);
}

エポールマンペイジが書いたように、私は取得するまで読み取り/書き直さなければなりません EAGAIN, 、しかし、私はすでにそれを手に入れました!そして、私は確かにバッファが空にならないことを確かです。私は何が間違っていますか?

UPD: 私は興味深いことを見つけました:そのような状況が起こったとき、私は自分のコードで使用します sleep(1)recc(...) 繰り返しますが、私は期待するデータを取得します!それは汚いトリックです。この問題を解決するためのより優雅なアプローチはありますか?

役に立ちましたか?

解決

最初は完全に正常です recv() その場合、戻る EAGAIN. epoll() まだ読みやすいかどうかは決して言わなかった。

一つ一つの recv() 処理する準備をする必要があります EAGAIN 非ブロッキングソケットを使用している場合。偽のウェイクアップが可能なので、APIが好きなときはいつでも select(), poll() また epoll() ソケットは読みやすいと言って、「それは「それ」と言っているだけです そうかもしれない 読みやすい - 試してみてください」。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top