Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top