Contrary to sauoq, I think using length-prefixed strings is an excellent method, very simple and efficient. The only disadvantage is that you can't use Telnet to debug it.

The BEEP protocol uses it, and Marshall Rose says it is the most efficient method. See RFC3117: On the Design of Application Protocols (a good read).

Bernstein uses strings preceded by an delimited decimal length string in netstrings.

Note that there are really several issues here which can be handled together or separately. First is the reader knowing how much data to expect in the next "chunk". Second, delimiting logical strings within a transaction. Third, knowing whose turn it is to talk. In your example, it appears issues one and two are combined, and the third issue is just handled by the client and server programs just knowing (e.g.) how many strings to accept. For simple protocols that's okay, but it's better to have some general method to indicate line turnaround (e.g. SMTP/POP error messages continue with a "-" as the fourth character in a line to indicate that there's more to come, and use a "." on a line by itself to indicate the end of a message.


In reply to Re: Choosing a client/server protocol by Thelonius
in thread Choosing a client/server protocol by Coruscate

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.