Modifiche varie e ripulitura esempi. Trattata accept4.
[gapil.git] / listati / TCP_echod_first_main.c
diff --git a/listati/TCP_echod_first_main.c b/listati/TCP_echod_first_main.c
new file mode 100644 (file)
index 0000000..3f69e89
--- /dev/null
@@ -0,0 +1,26 @@
+    ...
+    /* main body */
+    if (listen(list_fd, BACKLOG) < 0 ) { /* listen on socket */
+       PrintErr("listen error");
+       exit(1);
+    }
+    while (1) {                          /* handle echo to client */
+       len = sizeof(cli_add);
+       if ( (conn_fd = accept(list_fd, NULL, NULL)) < 0) { 
+           PrintErr("accept error");
+           exit(1);
+       }
+       if ( (pid = fork()) < 0 ) {      /* fork to handle connection */
+           PrintErr("fork error");
+           exit(1);
+       }
+       if (pid == 0) {      /* child */
+           close(list_fd);          /* close listening socket */   
+           ServEcho(conn_fd);       /* handle echo */
+           exit(0);
+       } else {             /* parent */
+           close(conn_fd);          /* close connected socket */
+       }
+    }
+    exit(0);     /* normal exit, never reached */
+}