Varie correzioni, completata revisione capitolo sull'I/O su file
[gapil.git] / listati / poll_echod.c
index 91b53cefee58f03f0a9ae6d4944800d24d5b01fc..87eeb9f084ed65040a9c77fd186f41206aa4d74b 100644 (file)
@@ -1,20 +1,17 @@
-    /* 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 */
@@ -25,7 +22,6 @@
            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 */
@@ -55,6 +51,4 @@
            }
        }
     }
-    /* normal exit, never reached */
-    exit(0);
-}
+...