2 memset(fd_open, 0, FD_SETSIZE); /* clear array of open files */
3 max_fd = list_fd; /* maximum now is listening socket */
5 while (1) { /* main loop, wait for connection and data inside a select */
6 FD_ZERO(&fset); /* clear fd_set */
7 for (i = list_fd; i <= max_fd; i++) /* initialize fd_set */
8 if (fd_open[i] != 0) FD_SET(i, &fset);
9 while ( ((n = select(max_fd + 1, &fset, NULL, NULL, NULL)) < 0)
10 && (errno == EINTR)); /* wait for data or connection */
11 if (n < 0) { /* on real error exit */
12 PrintErr("select error");
15 if (FD_ISSET(list_fd, &fset)) { /* if new connection */
16 n--; /* decrement active */
17 len = sizeof(c_addr); /* and call accept */
18 if ((fd = accept(list_fd, (struct sockaddr *)&c_addr, &len)) < 0) {
19 PrintErr("accept error");
22 fd_open[fd] = 1; /* set new connection socket */
23 if (max_fd < fd) max_fd = fd; /* if needed set new maximum */
25 i = list_fd; /* first socket to look */
26 while (n != 0) { /* loop on open connections */
27 i++; /* start after listening socket */
28 if (fd_open[i] == 0) continue; /* closed, go next */
29 if (FD_ISSET(i, &fset)) { /* if active process it*/
30 n--; /* decrease active */
31 nread = read(i, buffer, MAXLINE); /* read operations */
33 PrintErr("Errore in lettura");
36 if (nread == 0) { /* if closed connection */
37 close(i); /* close file */
38 fd_open[i] = 0; /* mark as closed in table */
39 if (max_fd == i) { /* if was the maximum */
40 while (fd_open[--i] == 0); /* loop down */
41 max_fd = i; /* set new maximum */
42 break; /* and go back to select */
44 continue; /* continue loop on open */
46 nwrite = FullWrite(i, buffer, nread); /* write data */
48 PrintErr("Errore in scrittura");