Hello Perl experts. I'm writing a Perl client/server program that operates over a remote shell (SSH) with a simple line-based text protocol. However, I need to also transmit blobs of binary data.

The client uses open2(*Reader, *Writer, "ssh ... my_server") to get a Reader and a Writer filehandle to the remote server.

The server simply reads <STDIN> and writes to <STDOUT>

This setup works fine for the text-based parts of the protocol.

I am not sure of the best way to do the binary transfer from client to server. I tried using syswrite() from the client and sysread() on the server (basically) as like this:

Client: Sends text "----- Chunk of length $len -----\n" Server: Receives above line OK. Parses correctly and calls sysread(STDIN, $buf, $len);

Client: Sends exactly $len bytes with syswrite(Writer, $data, $len)

Server: sysread() blocks indefinitely

If I send an extra "\n" after my chunk of data, sysread() unblocks, but I am concerned that if my binary data contains newlines that this may cause other problems.

I realise it is ill-advised to mix sysread()/syswrite() with other (buffered) IO types, and that this is part of the problem. Any suggestions on either how to make this work, or a better design?

Thank you!


In reply to Mixing sysread() with <FILEHANDLE>? by wanna_code_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.