Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Generally you would send the length first. Both ends should agree on what a length looks like - for example, you might be happy to use fixed 4-byte length prefix as binary:</p> <pre><code> byte[] data = ... int len = data.Length; byte[] prefix = Bitconverter.GetBytes(len); stream.Write(prefix, 0, prefix.Length); // fixed 4 bytes stream.Write(data, 0, data.Length); </code></pre> <p>Obviously the caller needs to do the same - i.e. read the first 4 bytes to get the length. For reading, the receiver should take care not to read too much data. One way is with a limiting stream - for example, <a href="http://code.google.com/p/protobuf-net/source/browse/trunk/protobuf-net/SubStream.cs" rel="nofollow noreferrer">this class</a> can be used to get a Stream that won't read too much.</p> <p>If you don't want the overhead of always sending 4 bytes, then some more interesting encodings are possible - for example, using the msb as a continuation block.</p> <p>For info, <a href="http://code.google.com/p/protobuf-net/" rel="nofollow noreferrer">protobuf-net</a> is a binary serializer designed around Google's "protocol buffers" message-based format. It handles a lot of the details for you, and might be of interest if you don't want to spend lots of time writing serialization code. There are examples for sockets in the QuickStart project, <a href="http://code.google.com/p/protobuf-net/source/browse/trunk/QuickStart/3%20Sockets.cs" rel="nofollow noreferrer">for example here</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload