Introduction

This is YACSQ (Yet Another Client/Server Question). After a long debate with myself, I have settled upon one of many options for how to go about setting up a Client/Server application. Getting data back and forth between the server and the client is complete (and of course is the easiest step IMO). Now I have stumbled upon what I believe will be the biggest challenge in the project: selecting a communication design (ie: the protocol).

What I have so far

The client/server setup I have chosen is quite simple indeed. The client listens on STDIN and on the socket simultaneously in order to create a non-blocking socket. The client can exchange data with the server even while the user is being slow at typing in the next line. It works excellent. The server loops through the clients and helps each one out as it sees fit. Great. But I don't know where to start to create the protocol that the client and server can use to say what data they are about to send.

What I've thought of

As another decision, I have decided to use sysread() and syswrite() for all communication between the two apps. Using this method means that newlines won't necessarily be the line terminator. As well, I can read as many bytes at a time as I wish (a good thing if I end up using a fixed-length protocol). I've thought of multiple ideas, none of which I'm sure I'd like to implement. I'm hoping someone has better ideas today than myself.

Idea #1: XML Tagged
Use an xml-based interface for communications. Every time the client or server wishes to say something, they pass along a block of text such as the below example. The server sends the first block to the client, requesting a password. The server then waits for a response.

# server sends to client <command name="get_passwd"> Please enter your password: </command> # client sending to server <command name="text_data"> example_password </command>


pros: looks clean, quite obvious as to what is going on.
cons: parsing the tags. Seems annoying. Help with other cons?

Idea #2: Fixed-Length Communication
Using a fixed-length protocol seems simple to me, so I must be missing something. I was thinking maybe of following the idea of the 'content-length' from HTTP. The example below captures my thoughts pretty well I think. The first two chars show the length of the command in 2-digit form ('get_passwd' is 10 chars long). Following the command is a 4-digit number specifying the length of the data to follow (I figure 9999 is the longest I'll ever send :P).

# server sends to client. syswrite( $client, "10get_passwd0027Please enter your password:" ); # client reads from server my ($len, $cmd, $data); sysread($server, $len, 2); sysread($server, $cmd, $len); sysread($server, $len, 4); sysread($server, $data, $len); # $cmd eq 'get_passwd' # this is # $data eq 'Please enter your password:' # correct, right?


pros: seems quite trivial. No parsing involved. Just multiple reads.
cons: can't think of any. Help?

So are my ideas out of this world in a bad way or do I have a good brainwave or two focused on this? Any insights would be greatly appreciated and welcomed with wide open arms. Slaps across the face for stupid thoughts are welcome as well :)


If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.


In reply to 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.