By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

Length prefixed network message format...easy way to read this?

P: n/a
Maybe I'm missing the obvious solution because I've been looking at
this problem for so long, so I figured I'd throw it out and see if
anyone can come up with some ideas. :)

I'm writing a server application using VB.NET that will be
communicating with a device...the format that it uses can't be
changed. The format is something like this:

Device: <length><message>
Server: <ack> (two null bytes, or "zero length")
Server: <length><message>
Device: <ack> (two null bytes, or "zero length")

Where <length> is a 16 bit integer (two bytes) that defines the length
of <message>. What I'm currently doing looks like a mess to me...it
works, but it's slow and I'm sure there's a better way of doing it.
Is there an easy way to read length-prefixed messages? I thought
about using the BinaryReader on top of my NetworkStream, but it uses a
variable length prefix (not set at 2 bytes). I also need to be able
to send and receive data at the same time, if that makes a big
difference.

Thanks in advance for any help you can offer!
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jeff L. <jl***@racoindustries.com> wrote:
Maybe I'm missing the obvious solution because I've been looking at
this problem for so long, so I figured I'd throw it out and see if
anyone can come up with some ideas. :)

I'm writing a server application using VB.NET that will be
communicating with a device...the format that it uses can't be
changed. The format is something like this:

Device: <length><message>
Server: <ack> (two null bytes, or "zero length")
Server: <length><message>
Device: <ack> (two null bytes, or "zero length")

Where <length> is a 16 bit integer (two bytes) that defines the length
of <message>. What I'm currently doing looks like a mess to me...it
works, but it's slow and I'm sure there's a better way of doing it.
Is there an easy way to read length-prefixed messages? I thought
about using the BinaryReader on top of my NetworkStream, but it uses a
variable length prefix (not set at 2 bytes). I also need to be able
to send and receive data at the same time, if that makes a big
difference.


That looks fine to me. Using a stream, just fetch the first two bytes
to find out how long the response is, and then read blocks until you've
read all of it (and no more). Similarly when you're writing, just write
the length in the first two bytes, and then write the message itself
(having previously converted it into bytes with a suitable Encoding
call). You could use BinaryWriter/BinaryReader to write/read the
message lengths, or just do them manually with bit-shifting.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

P: n/a
Jeff,
I thought
about using the BinaryReader on top of my NetworkStream,


Sounds like a great start. I'd probably do it like this

Dim stringLength As Short = binReader.ReadInt16();
If stringLength > 0 Then
Dim stringData() As Byte = binReader.ReadBytes(stringLength)
Dim msg As String = Encoding.SomeEncoding.GetString(stringData)
End If

where SomeEncoding is whatever encoding the message is in on the wire.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.