1 /* initialize all needed variables */
2 poll_set = (struct pollfd *) malloc(n * sizeof(struct pollfd));
3 max_fd = list_fd; /* maximum now is listening socket */
6 poll_set[i].events = POLLRDNORM;
8 poll_set[max_fd].fd = list_fd;
9 /* main loop, wait for connection and data inside a select */
11 while ( ((n = poll(poll_set, max_fd + 1, -1)) < 0)
12 && (errno == EINTR)); /* wait for data or connection */
13 if (n < 0) { /* on real error exit */
14 PrintErr("poll error");
18 if (poll_set[list_fd].revents & POLLRDNORM) { /* if new connection */
19 n--; /* decrement active */
20 len = sizeof(c_addr); /* and call accept */
21 if ((fd = accept(list_fd, (struct sockaddr *)&c_addr, &len)) < 0) {
22 PrintErr("accept error");
25 poll_set[fd].fd = fd; /* set new connection socket */
26 if (max_fd < fd) max_fd = fd; /* if needed set new maximum */
28 /* loop on open connections */
29 i = list_fd; /* first socket to look */
30 while (n != 0) { /* loop until active */
31 i++; /* start after listening socket */
32 if (poll_set[i].fd == -1) continue; /* closed, go next */
33 if (poll_set[i].revents & (POLLRDNORM|POLLERR)) {
34 n--; /* decrease active */
35 nread = read(i, buffer, MAXLINE); /* read operations */
37 PrintErr("Errore in lettura");
40 if (nread == 0) { /* if closed connection */
41 close(i); /* close file */
42 poll_set[i].fd = -1; /* mark as closed in table */
43 if (max_fd == i) { /* if was the maximum */
44 while (poll_set[--i].fd == -1); /* loop down */
45 max_fd = i; /* set new maximum */
46 break; /* and go back to select */
48 continue; /* continue loop on open */
50 nwrite = FullWrite(i, buffer, nread); /* write data */
52 PrintErr("Errore in scrittura");
58 /* normal exit, never reached */