How to get errno when epoll_wait returns EPOLLERR?

女生的网名这么多〃 提交于 2019-12-03 06:08:58

Use getsockopt and SO_ERROR to get the pending error on the socket

int       error = 0;
socklen_t errlen = sizeof(error);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == 0)
{
    printf("error = %s\n", strerror(error));
}

Just a minor point: Your test won't work correctly, for two reasons. If EPOLLERR is defined as, say, 0x8, then your test will be comparing 8 with one (since == has higher precedence than &), giving you a zero, then anding that with the event mask.

What you want is: (epoll_event.events & EPOLLERR) != 0 to test for the EPOLLERR bit being set.

epoll_wait returns -1 when an error occurs and sets errno appropriately. See "man 2 epoll_wait" for more info.

Include errno.h and use perror to see the error message. Basically error is from the epfd or interupt, it will not arise from the file descriptor in your set.

include "errno.h"

if(epoll_wait() == -1)
    {
      perror("Epoll error : ");
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!