10

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

5 Answers 5

11

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
2
  • The "Server" line fails for me. It complains that -w must be greater than 0 and that -k is not supported in UDP mode except with --exec and --exec-sh. no big deal I can set -w to 1 but not sure about the -k error. Anyone else seeing that?
    – Dan
    Jun 7, 2019 at 1:32
  • @Dan : Yes, -k is not supported with UDP in the "classic" version, or in ncat. And -w0 is also only supported in the OpenBSD version. See also my own answer.
    – mivk
    Dec 19, 2021 at 16:45
0

I found a link that may help you: netcat: Strange behaviour with UDP – only receives first packet sent.

At this stage I wasn’t sure what to do but Nathan pointed out that if I made use of timeouts on the client and server then it would be possible to send multiple UDP packets.

I restarted the netcat server but this time with a 1 second timeout:

$ nc -kluvw 1 localhost 9000

And then started sending UDP packets from a netcat client also with a 1 second timeout:

 $ echo -e "all" | nc -vvuw 1 localhost 9000
 Connection to localhost 9000 port [udp/cslistener] succeeded!
 $ echo -e "the" | nc -vvuw 1 localhost 9000
 Connection to localhost 9000 port [udp/cslistener] succeeded!
 $ echo -e "udp" | nc -vvuw 1 localhost 9000
 Connection to localhost 9000 port [udp/cslistener] succeeded!
 $ echo -e "packets" | nc -vvuw 1 localhost 9000
 Connection to localhost 9000 port [udp/cslistener] succeeded!

And the netcat server now receives all of them:

$ nc -kluvw 1 localhost 9000
XXXXall
XXXXthe
XXXXudp
XXXXpackets

This was an interesting problem, hope this answers your question.

0

For monitoring an UDP port, you can try

netcat -lzu -p 9000

# -z, --zero                 zero-I/O mode (used for scanning)

This works in my GNU netcat 0.7.1.

0

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.
0

I currently get:

Ncat: Version 7.50 ( https://nmap.org/ncat ) Ncat: UDP mode does not support the -k or --keep-open options, except with --exec or --sh-exec. QUITTING.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .