I guess this is close to linux - Netcat stops listening for UDP traffic - Super User, but I thought I'd better ask anyways
As far as versions of netcat I'm using Ubuntu 11.04 and the default
netcat on it, which I'm guessing is the
$ nc This is nc from the netcat-openbsd package. An alternative nc is available in the netcat-traditional package. usage: nc [-46DdhklnrStUuvzC] [-i interval] [-P proxy_username] [-p source_port] [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [hostname] [port[s]]
This is what I find strange: the first case works as expected - I open a UDP server in one terminal:
$ sudo nc -ul 5000
... and in another terminal I initiate a new UDP client connection - and I type in
hello three times, pressing ENTER after each:
$ nc -u 127.0.0.1 5000 hello hello hello ^C
... and going back to the server terminal, it has printed
hello three times, as expected:
$ sudo nc -ul 5000 hello hello hello ^C
So far so good, all works as expected. However, let's say I try now the same by piping input into the client; so establish first a UDP server in one terminal:
$ sudo nc -ul 5000
... and in another, pipe some data into
nc as UDP client:
$ echo hello | nc -u 127.0.0.1 5000 hello hello ^C
... after the client command, the shell sort of freezes as if waiting for input - so there I type
hello and ENTER two more times; but the server has registered only the first
hello (which was piped via
echo). Furthermore, even if you press Ctrl-C, and try to repeat the client
echo hello | nc -u 127.0.0.1 5000 command, the server will still remain on having reported just the very first
$ sudo nc -ul 57130 hello ^C
... and only after stopping the server with Ctrl-C and restarting it again, can one repeat the client
echo hello | nc -u 127.0.0.1 5000 command and observe it working.
Is this the way
nc is supposed to be behaving? I would expect that at least repeated calls to
echo hello | nc -u 127.0.0.1 5000 would be registered - without having to restart the server? Or maybe there is a special command-line switch for that kind of behavior?
EDIT: I found this nice PDF presentation: socat – Handling all Kinds of Sockets, which contains the following
netcat - Limitations
● one-shot only (terminates after socket close)
Examples 1: netcat Replacement
● UDP client with source port:
nc -u -p 500 184.108.40.206 500
socat - udp:220.127.116.11:500,sp=500
● TCP server:
nc -l -p 8080
socat - tcp-l:8080,reuseaddr
... however, I'm pretty much getting the same behavior as above, if I replace the server command with "
socat - udp4-listen:5000,reuseaddr" - and the client line with "
socat - udp:127.0.0.1:5000"... With the piped input, "
echo hello | socat - udp:127.0.0.1:5000", the only difference is that here the command at least exists after the word
hello had been sent - however, again, consecutive runs of this command will not cause any reception at the server, until the server is restarted.