Nc (netcat) hangs, waiting for more data, in UDP mode.

netcatshellstatsd

I'm trying to send a small string to statsd via nc inside of a read block:

while read line; do
    printf "folder.counter:value|1c" | nc -q 0 -u $host $port
done

Unfortunately, when in UDP mode, nc seems to want to wait indefinitely, even though I've specified -q 0, which the man page says will make the program exit immediately after EOF.

I've tried passing -w 1, but if the data I'm sending comes in at more than one line per second, the data buffers up, and I lose my real time stats (not to mention risking a buffer overflow of some sort).

Is it possible to do what I'm trying to do with netcat, or am I going to need to write something in language which has a statsd library?

Best Answer

I ended up fixing the problem by switching to socat:

while read line; do
    printf "folder.counter:value|1c" | socat -t 0 - UDP:$host:$port
done