Commentato il codice del server echo basato sulla funzione poll.
[gapil.git] / listati / TCP_echod_second.c
1 int main(int argc, char *argv[])
2 {
3     ...
4     int waiting = 0;
5     int compat = 0;
6     ...
7
8     /* Main code begin here */
9     if (compat) {                             /* install signal handler */
10         Signal(SIGCHLD, HandSigCHLD);         /* non restarting handler */
11     } else {
12         SignalRestart(SIGCHLD, HandSigCHLD);  /* restarting handler */
13     }
14     ...
15
16     /* main body */
17     if (listen(list_fd, BACKLOG) < 0 ) {
18         PrintErr("listen error");
19         exit(1);
20     }
21     if (waiting) sleep(waiting);
22     /* handle echo to client */
23     while (1) {
24         /* accept connection */
25         len = sizeof(cli_add);
26         while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len)) 
27                 < 0) && (errno == EINTR)); 
28         if ( conn_fd < 0) {
29             PrintErr("accept error");
30             exit(1);
31         }
32         if (debugging) {
33             inet_ntop(AF_INET, &cli_add.sin_addr, ipaddr, sizeof(ipaddr));
34             snprintf(debug, MAXLINE, "Accepted connection form %s\n", ipaddr);
35             if (demonize) {
36                 syslog(LOG_DEBUG, debug);
37             } else {
38                 printf("%s", debug);
39             }
40         }
41         /* fork to handle connection */
42         ...
43     }
44     return;
45 }