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

  • Thread starter Thread starter Jeff L.
  • Start date Start date
J

Jeff L.

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!
 
Jeff L. said:
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.
 
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
 
Back
Top