通过epoll处理监听套接字

Je *_*Rog 3 c sockets epoll

以下全部来自man epoll页面:

函数do_use_fd()使用新的就绪文件描述符,直到EAGAIN由read(2)或write(2)返回。

ET触发的代码示例:

   for(;;) {
       nfds = epoll_wait(kdpfd, events, maxevents, -1);

       for(n = 0; n < nfds; ++n) {
           if(events[n].data.fd == listener) {
               client = accept(listener, (struct sockaddr *) &local,
                               &addrlen);
               if(client < 0){
                   perror("accept");
                   continue;
               }
               setnonblocking(client);
               ev.events = EPOLLIN | EPOLLET;
               ev.events = EPOLLIN | EPOLLET;
               ev.data.fd = client;
               if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, client, &ev) < 0) {
                   fprintf(stderr, "epoll set insertion error: fd=%d\n",
                           client);
                   return -1;
               }
           }
           else
               do_use_fd(events[n].data.fd);
       }
   }
Run Code Online (Sandbox Code Playgroud)

因此,对于read/write操作,我们应该循环执行直到EAGAIN收到a;但是为什么不是这样accept呢?

IMO上面的代码在有多个客户端套接字等待被接受时会丢失一些请求,因为它只接受1个客户端套接字,因此我们也应该将其包装成一个循环,直到被EAGAIN接收。

或者也许我缺少什么?

Mat*_*Mat 5

查看如何将侦听器套接字添加到epollfd

ev.events = EPOLLIN;       // this is the crucial bit
ev.data.fd = listen_sock;
Run Code Online (Sandbox Code Playgroud)

它不是在边沿触发中添加的,而是在水平触发中添加的。因此,在该循环之前无需循环EAGAIN