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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #define READ_ONLY ( POLLIN | POLLPRI )
#define POLL_ERROR ( POLLERR | POLLHUP | POLLNVAL )
#define POLL_OUTPUT ( POLLOUT )
socket...; bind...; listen...; int i=0; struct pollfd pollfds[MAXCN]; for(i=0;i<MAXCN; i++) pollfds[i].fd = -1;
pollfds[0].fd = serverfd; pollfds[0].events = READ_ONLY;
setNonBlocking(serverfd);
int timeout = 30000; int maxfd=0; while(1) { int ret = poll(pollfds, static_cast<unsigned long>(maxfd+1),timeout); if(ret < 0) { break; } else if(ret == 0) { continue; } if(pollfds[0].revents & POLLIN) { int clientfd = accept... if(maxfd >= MAXCN) { } else { setNonBlocking(clientfd); } } for() { if(pollfds[i].revents & (READ_ONLY | POLL_ERROR)) callReadBackFunC(); } }
|