You need to select a port, and always send from that port.
As explained here, after a listening UDP port got it's first packet, it will only accept additional input if it comes from the same sending port.
So, given a sending port, a listening port and a listening host:
send_port=7777
listen_port=8888
host=127.0.0.1
And a listener started with :
nc -lu -p $listen_port
The sender must then use the -p
option to always use the same source port. This will work and print all 4 lines on the listener :
for c in {1..4}; do
echo "line $c" | nc -u -w 0 -p $send_port $host $listen_port
done
Also, note that different versions of netcat have different rules about their options:
- the original netcat (
netcat-traditional
in Debian-based distributions) doesn't support a timeout of 0 (-w 0
), so you need to use -w 1
instead.
- Nmap's
ncat
doesn't either, but supports fractional timeouts like -w 0.1
- In listener mode, the
-p
before the listening port is only needed in the original nc
v. 1.10. Other versions just take the last argument as the listening port. But all accept it also as -p $listen_port
.