epoll_ctl()返回EINVAL

时间:2019-07-18 17:35:41

标签: c++ epoll

我正在尝试编写epoll TCP服务器的代码,我不确定epoll_ctl为什么返回errno(EINVAL)。

根据手册: “ EINVAL-参数epfd无效,或者与fd相同,或者不支持由参数op指定的操作。”

  1. 参数epfd无效:epoll_create()返回0,即 有效。
  2. 它与fd相同:gdb显示它们不同。
  3. 不支持由参数op指定的操作:EPOLL_CTL_ADD

代码:

struct epoll_event ep_env;
ep_env.data.fd = mServerFd;
ep_env.events  = EPOLLIN;
if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mServerFd, &ep_env) == -1)
{
    std::cerr << "ERROR: TCPServer::epollController() failed."
            << std::endl;
    std::cerr << "ERRNO: " << errno << " - " << std::endl;
    if (errno == EINVAL)
    {
        std::cout << "EINVAL" << std::endl;
    }
    else if (errno == EBADF)
    {
        std::cout << "EBADF" << std::endl;
    }
    else
    {
        std::cout << "else" << std::endl;
    }
}

gdb:
[New Thread 0x7ffff6fd2700 (LWP 28133)]
Server initialized. Listening on port 5000...
[Switching to Thread 0x7ffff6fd2700 (LWP 28133)]

Thread 2 "messagePort" hit Breakpoint 1, TCPServer::run (this=0x60d0a0)
    at .../TCPServer.cpp:169
169         if (epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mServerFd, &ep_env) == -1)
(gdb) print mServerFd
$1 = 3
(gdb) print mEpollFd
$2 = 0
(gdb) print ep_env
$3 = {events = 1, data = {ptr = 0x3, fd = 3, u32 = 3, u64 = 3}}
(gdb) step
171             std::cerr << "ERROR: TCPServer::epollController() failed."
(gdb)


Output from exe:
Server initialized. Listening on port 5000...
ERROR: TCPServer::epollController() failed.
ERRNO: 22 - EINVAL

感谢任何输入!

0 个答案:

没有答案