X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=listati%2FTCP_echod_first_main.c;fp=listati%2FTCP_echod_first_main.c;h=3f69e89c25626f54161dbf2d065f6f1f7568fc44;hb=541dec587ab21a1e01342784a803ab6404b98d1e;hp=0000000000000000000000000000000000000000;hpb=5077cb02e6a65499d7fddcd01d68e8800c73a6fa;p=gapil.git diff --git a/listati/TCP_echod_first_main.c b/listati/TCP_echod_first_main.c new file mode 100644 index 0000000..3f69e89 --- /dev/null +++ b/listati/TCP_echod_first_main.c @@ -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 */ +}