Materiale dimenticato e la versione preliminare del server echo implementato
[gapil.git] / listati / select_echod.c
1     ...
2     memset(fd_open, 0, FD_SETSIZE);   /* clear array of open files */
3     max_fd = list_fd;                 /* maximum now is listening socket */
4     fd_open[max_fd] = 1;
5     /* main loop, wait for connection and data inside a select */
6     while (1) {  
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); 
10         }
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");
15             exit(1);
16         }
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");
22                 exit(1);
23             }
24             fd_open[fd] = 1;                  /* set new connection socket */
25             if (max_fd < fd) max_fd = fd;     /* if needed set new maximum */
26         }
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 */
35                 if (nread < 0) {
36                     PrintErr("Errore in lettura");
37                     exit(1);
38                 }
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 */
46                     }
47                     continue;                /* continue loop on open */
48                 }
49                 nwrite = FullWrite(i, buffer, nread); /* write data */
50                 if (nwrite) {
51                     PrintErr("Errore in scrittura");
52                     exit(1);
53                 }
54             }
55         }
56     }
57     ...