Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / listati / TCP_echod_first_main.c
1     ...
2     /* main body */
3     if (listen(list_fd, BACKLOG) < 0 ) { /* listen on socket */
4         PrintErr("listen error");
5         exit(1);
6     }
7     while (1) {                          /* handle echo to client */
8         len = sizeof(cli_add);
9         if ( (conn_fd = accept(list_fd, NULL, NULL)) < 0) { 
10             PrintErr("accept error");
11             exit(1);
12         }
13         if ( (pid = fork()) < 0 ) {      /* fork to handle connection */
14             PrintErr("fork error");
15             exit(1);
16         }
17         if (pid == 0) {      /* child */
18             close(list_fd);          /* close listening socket */   
19             ServEcho(conn_fd);       /* handle echo */
20             exit(0);
21         } else {             /* parent */
22             close(conn_fd);          /* close connected socket */
23         }
24     }
25     exit(0);     /* normal exit, never reached */
26 }