You do it with the -k
option to nc
.
-k Forces nc to stay listening for another connection after its cur-
rent connection is completed. It is an error to use this option
without the -l option. When used together with the -u option,
the server socket is not connected and it can receive UDP data-
grams from multiple hosts.
Example:
$ rm -f /tmp/socket # unlink the socket if it already exists
$ nc -vklU /tmp/socket # the server
Connection from mack received!
yes
Connection from mack received!
yes
...
It's recommended to unlink()
the socket after use -- but, in fact, most programs check if it exists and remove it before calling bind()
on it; if the socket path exists in the filesystem and you try to bind()
to it, you will get an EADDRINUSE
error even when no program is using it in any way.
One way to avoid this whole mess on linux is to use "abstract" unix sockets, but they don't seem to be supported by netcat
.
nc
you are trying to have a "server" listening on the socket while something else is already listening to it. In the first case, a singleacpid
instance is listening on the socket. In both cases, multiple clients can write to it. (All this written with an appropriate amount of handwaving). – Kusalananda♦ Sep 21 '18 at 9:27nc
allows only single-use sockets. What's true, is thatnc
is not a real server (it will notfork
a subprocess to handle a connection, then go back to accepting connections while the child is still running, like eg. the X11 server oracpid
do); whilenc -l
is handling a connection, it will notaccept()
other connections on the same socket. And this is no different from hownc -l
works withtcp
sockets. – mosvy Sep 21 '18 at 12:50nc
process just to allow multiple other listeners ? – Sergiy Kolodyazhnyy Sep 21 '18 at 15:12