Without the -q
flag your instance of netcat
will wait forever. There's no "end of stream" message with UDP so there is no way for netcat
to know that both stdin and the network connection have finished.
For example, using TCP/IP this works as expected:
nc -l localhost 4300 # Window 1
nc localhost 4300 </etc/group # Window 2
But as you have determined, using UDP/IP this never ends:
nc -u -l localhost 4300 # Window 1
nc -u localhost 4300 </etc/group # Window 2
This is where the -q
flag comes in. But unfortunately it doesn't accept a value of 0
. It also won't accept non-integer values. Here is the best alternative I can offer without recourse to timeout
or some other external utility:
nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
Even here, it's not possible to have the listening netcat
time out gracefully. (The -w
timeout option is ignored, and -q
is irrelevant.) Something like this might be of use in a practical situation, so that the netcat
is killed after 90 seconds:
timeout 90 nc -u -l localhost 4300 # Window 1
nc -q 1 -u localhost 4300 </etc/group # Window 2
-q 0
flag with the pipe solution (withcat
) instead of with the redirection solution (<
) – Camion Feb 07 '22 at 14:30