5

I have a situation where many "smart" devices are sending me data via UDP. I can't change anything on the sending end.

I'm testing nc (or netcat) but can't get past the following:

Receiving end command:

nc -l -u 8123

Test sending command:

echo "test" | nc -u 127.0.0.1 8123

The first packet works fine, but then both ends seem to go to some sort of sleep and I have to CTRL+C to try again. Once it works I will have the receiving end "keep listening" (-k) and I will process the packets then

AReddy
  • 3,122
  • 5
  • 35
  • 75
Scott May
  • 73
  • 1
  • 5

4 Answers4

2

The answer, it if comes up - there's apparently some issue with netcat when used like this - socat provided a solution:

Receive:

socat -u udp4-recv:8123 - 

Send:

echo "test" | socat - udp4-sendto:127.0.0.1:8123
Scott May
  • 73
  • 1
  • 5
1

server side: nc -ul 127.0.0.1 1234 - listen to udp connection on port 1234

also: nc -l 1234

client side: nc -u 127.0.0.1 1234 - connect using udp to 127.0.0.1, port 1234

Now type something on the client side and you see it on the server side. (You can also type on the server side, and it will get through to the client.)

When you do echo "test" | nc -u 127.0.0.1 8123, the server side stays as before, but now the standard input of nc on the client side is bound to the pipe descriptor. When you type on the keyboard, it goes nowhere. nc is waiting for input from the pipe, and the pipe has already sent all it had to send. So now nc is "frozen".

1

According to the man page, you need to pass the -q0 flag to nc to make it exit when finished.

Sod Almighty
  • 133
  • 5
0

I was trying to test some DNS stuff, and the thing that was missing for me was the -d flag. Full command, using BSD nc on Ubuntu 22.04:

nc -d -l -q 0 -u 127.0.0.1 8123

Add -v for debugging through stderr.

Ainar-G
  • 347
  • 2
  • 8