On Tue, Sep 04, 2018 at 01:01:38PM +0800, Nan Xiao wrote: > Before netcat program exits, it will check whether s is -1, and close > socket if s is not -1: > > if (s != -1) > close(s); > > The following patch fixes the issue that netcat will close socket twice > if it works as a server:
I think it is a bug, but should be fixed differently. The netstat code has a lot of if and else for all the use cases. Adding a s = -1 at one place makes it even more confusing. In general main() does not reset s to -1, it isets it at the beginning. Look at the client side. There we close at the beginning of the loop: /* Cycle through portlist, connecting to each port. */ for (s = -1, i = 0; portlist[i] != NULL; i++) { if (s != -1) close(s); So on the server side we should do the same, otherwise the code will get more and more messy. Use the same concept everywhere. I think this would be better: /* Allow only one connection at a time, but stay alive. */ for (;;) { if (family != AF_UNIX) { if (s != -1) close(s); s = local_listen(host, uport, hints); } bluhm