- /* initialize all needed variables */
+...
poll_set = (struct pollfd *) malloc(n * sizeof(struct pollfd));
max_fd = list_fd; /* maximum now is listening socket */
- for (i=0; i<n; i++) {
+ for (i=0; i<n; i++) { /* initialize poll set */
poll_set[i].fd = -1;
poll_set[i].events = POLLRDNORM;
}
- poll_set[max_fd].fd = list_fd;
- /* main loop, wait for connection and data inside a select */
- while (1) {
- while ( ((n = poll(poll_set, max_fd + 1, -1)) < 0)
- && (errno == EINTR)); /* wait for data or connection */
+ poll_set[max_fd].fd = list_fd;
+ while (1) { /* main loop, wait for connection and data inside a poll */
+ while ( ((n=poll(poll_set, max_fd + 1, -1)) < 0) && (errno == EINTR));
if (n < 0) { /* on real error exit */
PrintErr("poll error");
exit(1);
}
- /* on activity */
if (poll_set[list_fd].revents & POLLRDNORM) { /* if new connection */
n--; /* decrement active */
len = sizeof(c_addr); /* and call accept */
poll_set[fd].fd = fd; /* set new connection socket */
if (max_fd < fd) max_fd = fd; /* if needed set new maximum */
}
- /* loop on open connections */
i = list_fd; /* first socket to look */
while (n != 0) { /* loop until active */
i++; /* start after listening socket */
}
}
}
- /* normal exit, never reached */
- exit(0);
-}
+...