473,378 Members | 1,507 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,378 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 1966
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.