473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
2 1968
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Boyd Reilly | last post by:
I have a form that has the user pick the type of question he will answer. The input field will be a text, numeric or date type. So, after the question is answered, I need to change the input...
6
by: moonriver | last post by:
I write a program accessing files in network drive o:. It is doable as a standalone application. However, if it is running under windows service, the following exception will appear: 13/07/2004...
2
by: Kueishiong Tu | last post by:
I have a network stream which I got from HttpWebResponse and does not support seeking, How do I find the length of the network stream? HttpWebResponse* response = dynamic_cast<HttpWebResponse*>...
14
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I...
3
by: ecov | last post by:
Is there any easy way to read into a dataset a file that was created from a BCP command. The file is in a fixed length format. I would also like to be able to write out a dataset to the same type...
5
by: Mitchell S. Honnert | last post by:
Is there a way, given the full path of a folder on a network, that one can programatically tell if you have Read access to that folder? I have an application where the user is able to select a...
2
by: Jeff L. | last post by:
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...
4
by: Hone | last post by:
I'm trying to serialize/deserialize the XML for an RSS 1.0 Feed (i.e. RDF) and the root element, as required by the standard, looks like this: <rdf:RDF ...> </rdf:RDF> However, I've tried...
1
by: peary | last post by:
Hi, everyone, I'm writing a program to discover wireless network using Windows Native Wifi API & VB.net. I have to declare the windows API in my VB.net program. The original windows...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.