1

I would like to use netcat to listen to data and receive only ONE packet. How does one do this?

Xofo
  • 639
  • I don't think you can do that with nc. If you're actually "listening" with nc, it's a stream socket, and message boundaries are not preserved, so you cannot be sure that a single read won't be assembled from multiple packets (or vice-versa). If instead you're using the -u option to bind to a datagram socket, then a) you won't be able to receive any data from clients that use tcp/stream sockets b) each read(2) or recv(2) syscall will return a packet, but good luck convincing any shell utility to do a single read (not up to newline, some delimiter or a fixed length). –  Apr 04 '19 at 08:07
  • 1
    Thanks. It is UDP data and I wanted to get one read of the data. socat may be the way to go. – Xofo Apr 04 '19 at 18:58

1 Answers1

1

You can do nc -ulp 1234 -q1 < /dev/null to receive just the first one.

It's not great, as it relies on the timeout, but it should work unless your system is completely overloaded. Good enough for a one-off.

Thomas
  • 218