0

我在 linux 中有以下 C++ 代码:

if (epoll_wait(hEvent,&netEvents,1,0))
{
        // check FIRST for disconnection to avoid send() to a closed socket (halts on centos on my server!)
        if ((netEvents.events & EPOLLERR)||(netEvents.events & EPOLLHUP)||(netEvents.events & EPOLLRDHUP)) {
            save_log("> client terminated connection");
            goto connection_ended;              // ---[ if its a CLOSE event .. close :)
        }
        if (netEvents.events & EPOLLOUT)                 // ---[ if socket is available for write
        {
            if (send_len) {
                result = send(s,buffer,send_len,MSG_NOSIGNAL);
                save_slogf("1112:send (s=%d,len=%d,ret=%d,errno=%d,epoll=%d,events=%d)",s,send_len,result,errno,hEvent,netEvents.events);
                if (result > 0) {
                    send_len = 0;
                    current_stage = CL_STAGE_USE_LINK_BRIDGE;
                    if (close_after_send_response) {
                        save_log("> destination machine closed connection");
                        close_after_send_response = false;
                        goto connection_ended;
                    }
                } else {
                    if (errno == EAGAIN) return;
                    else if (errno == EWOULDBLOCK) return;
                    else {
                        save_log("> unexpected error on socket, terminating");
        connection_ended:
                        close_client();
                        reset();
                        return;
                    }
                }
            }
        }
    }
}

hEvent: epoll 创建监听 EPOLLIN,EPOLLOUT,EPOLLERR,EPOLLHUP,EPOLLRDHUP

s: NON-BLOCKING (!!!) 套接字从非阻塞侦听套接字上的接受创建

基本上,此代码试图将数据包发送回连接到服务器的已连接用户。它通常可以正常工作,但在随机情况下(也许当一些奇怪的网络事件发生时)程序无限期地挂在“result = send(s,buffer,send_len,MSG_NOSIGNAL) 行上。

我不知道这可能是什么原因,我试图监视套接字操作,但似乎没有任何线索告诉我为什么会发生这种情况。我不得不假设这是一个内核错误或非常奇怪的东西,因为我在 Windows 下编写了相同的程序并且它在那里完美运行。

4

0 回答 0