I would like to use netcat to listen to data and receive only ONE packet. How does one do this?
Asked
Active
Viewed 1,121 times
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.
nc
. If you're actually "listening" withnc
, 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