1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| int epollfd = epoll_create(1024); struct epoll_event ev; ev.events = EPOLLIN; ev.data.fd = fd; epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); struct epoll_event events_in[16]; while(1) { int event_count = epoll_wait(epollfd, events_in, 16, -1); for (int i = 0; i<event_count; i++) { if (events_in[i].data.fd == fd) { int new_fd = accept(fd, NULL, NULL); ev.events = EPOLLIN; ev.data.fd = new_fd; epoll_ctl(epollfd, EPOLL_CTL_ADD, new_fd, &ev); } else { int new_fd = events_in[i].data.fd; epoll_ctl(epollfd, EPOLL_CTL_DEL, new_fd, NULL); close(new_fd); } } }
|