78557afdb04fd9f9a16e3a9fb406d59b3ac9f1f4
[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         while (((conn_fd = accept(list_fd, (struct sockaddr *)&cli_add, &len)) 
26                 < 0) && (errno == EINTR)); 
27         if ( conn_fd < 0) {
28             PrintErr("accept error");
29             exit(1);
30         }
31         if (debugging) {
32             inet_ntop(AF_INET, &cli_add.sin_addr, ipaddr, sizeof(ipaddr));
33             snprintf(debug, MAXLINE, "Accepted connection form %s\n", ipaddr);
34             if (demonize) {
35                 syslog(LOG_DEBUG, debug);
36             } else {
37                 printf("%s", debug);
38             }
39         }
40         /* fork to handle connection */
41         ...
42     }
43     return;
44 }