views:

38

answers:

2

The object is essentially BYTEs of dynamic length.

What's the easiest way to implement a send/receive mechanism?

I got a tip follows but have no idea how to roll this protocol myself:

Just remember that pipes, like sockets, don't guarantee that everything you put in the pipe will come out the other end in the same number of reads as there were writes. When you read from the pipe or socket you are given what is currently available in the buffer which may not yet be everything that was written so you have to keep reading until you get the expected amount of data.

+3  A: 

How dirt cheep must this be?

Simplest case:

  • prepend byte count to data sent
  • keep reading until byte count reached
Daren Thomas
How to distinguish between byte count and the actual byte data at the receiving side?
Alan
Say first 4 bytes are byte count as a 32 bit unsigned integer. Specify endianess, bonus points for using the net byte ordering (or whatever it's called)
Daren Thomas
+1  A: 

How to distinguish between byte count and the actual byte data at the receiving side?

There are two ways.

The first is positional: if the first byte is the length 'n', and the next n bytes are data, then the byte after that is the start of the next packet, and is therefore the initial length byte of the next packet.

The second way is to use some escape character, to mark the start or end of each message.

ChrisW
... which are the main reasonable ways how strings of `char`, `int`, `long`, whatever, are implemented. And pipes are just a mechanism to pass data in a sequential fashion (i.e., strings!).
Eduardo León