我在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:从非阻塞侦听套接字上的接受创建的非阻塞(!!!)套接字
基本上,此代码尝试将数据包发送回连接到服务器的已连接用户。它通常可以正常工作,但在RANDOM场合(可能是一些奇怪的网络事件发生时),程序无限期地挂起“result = send(s,buffer,send_len,MSG_NOSIGNAL)行。
我不知道造成这种情况的原因是什么,我试图监视套接字操作,似乎没有任何东西给我一个线索,说明它为什么会发生。我必须假设这是一个KERNEL错误或者非常奇怪的东西,因为我在Windows下编写了相同的程序并且在那里工作得很完美。