Make netcat listen for multiple UDP packets

netcat

If I run a simple UDP listener like this:

nc -l -u -p 1234

Then I appear to only get the first inbound UDP packet. For example if I run:

$ echo abc | nc -u localhost 1234
  ["abc" appears in output of server as expected]

$ echo abc | nc -u localhost 1234
read(net): Connection refused

Best Answer

using a timeout of zero (0)

"Server":

nc -kluvw 0 localhost 9000

"Client":

echo -e "all"     | nc -vuw 0 localhost 9000
echo -e "the"     | nc -vuw 0 localhost 9000
echo -e "udp"     | nc -vuw 0 localhost 9000
echo -e "packets" | nc -vuw 0 localhost 9000

Result:

Connection from 127.0.0.1 port 9000 [udp/*] accepted
all
Connection from 127.0.0.1 port 9000 [udp/*] accepted
the
Connection from 127.0.0.1 port 9000 [udp/*] accepted
udp
Connection from 127.0.0.1 port 9000 [udp/*] accepted
packets

tested with:

uname -a
Linux ubuntu 3.2.0-23-generic-pae #36-Ubuntu SMP Tue Apr 10 22:19:09 UTC 2012 i686 i686 i386 GNU/Linux
Related Topic