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