3

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?

Ivan
  • 63
  • 1
  • 5
  • 1
    Why do you need to send it as binary? hex should work just fine. Binary is actualy taking the number of bytes needed and multiplying it by 4 (hex is 2 digs per byte, binary is 8 per byte, 2/8=4) – phyrrus9 Apr 28 '14 at 23:20
  • @phyrrus9 I think the OPs definition here of "binary" is a stream of 8-bit bytes, and not a stream of ascii 1s and 0s. For example, try `echo 737461636b6f766572666c6f772e636f6d0a | xxd -p -r` – Digital Trauma Apr 29 '14 at 00:15

1 Answers1

3

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
Digital Trauma
  • 15,475
  • 3
  • 51
  • 83