From: Aron Xu Date: Tue, 14 Feb 2012 23:02:00 +0800 Subject: serialized handling multiple clients --- netcat.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/netcat.c b/netcat.c index f4d85af..5d3a2d3 100644 --- a/netcat.c +++ b/netcat.c @@ -459,26 +459,24 @@ main(int argc, char *argv[]) s = unix_bind(host); else s = unix_listen(host); - } + } else + s = local_listen(host, uport, hints); + if (s < 0) + err(1, NULL); + + char* local; + if (family == AF_INET6) + local = ":::"; + else + local = "0.0.0.0"; + fprintf(stderr, "Listening on [%s] (family %d, port %d)\n", + host ?: local, + family, + *uport); /* Allow only one connection at a time, but stay alive. */ for (;;) { - if (family != AF_UNIX) - s = local_listen(host, uport, hints); - if (s < 0) - err(1, NULL); - char* local; - if (family == AF_INET6 ) - local = "0.0.0.0"; - else if (family == AF_INET) - local = ":::"; - else - local = "unknown"; - fprintf(stderr, "Listening on [%s] (family %d, port %d)\n", - host ?: local, - family, - *uport); /* * For UDP and -k, don't connect the socket, let it * receive datagrams from multiple socket pairs. @@ -525,15 +523,16 @@ main(int argc, char *argv[]) close(connfd); } - if (family != AF_UNIX) + if (kflag) + continue; + if (family != AF_UNIX) { close(s); + } else if (uflag) { if (connect(s, NULL, 0) < 0) err(1, "connect"); } - - if (!kflag) - break; + break; } } else if (family == AF_UNIX) { ret = 0; --