Reputation: 63
I need to read hexadecimal data from stdin, convert it to binary, send with netcat
, recieve reply, convert back to hex and print to stdout. I do:
# xxd -r -p | nc -u localhost 12345 | xxd
Then type my data in hex and press Enter. But it is not sent untill I press Ctrl+D
, so I'm unable to sent another packet after receiving reply. Looks like xxd -r -p
doesn't write binary data, until EOF
is given. Is there a way to make it write after newline?
Upvotes: 3
Views: 1090
Reputation: 16016
By default, most *nix utilities will do line buffering when in interactive mode (e.g. stdin/stdout connected directly to the terminal emulator). But when in non-interactive mode (e.g. stdin/stdout connected to a pipe) larger buffers are typically used - I think 8k or so is typical, but this varies largely by implementation/distro.
You can force buffering for a given process to line mode using the GNU stdbuf
utility, if you have it available:
stdbuf -oL xxd -r -p | nc -u localhost 12345 | xxd
Upvotes: 3