473,794 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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><messag e>
Server: <ack> (two null bytes, or "zero length")
Server: <length><messag e>
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 1996
Jeff L. <jl***@racoindu stries.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><messag e>
Server: <ack> (two null bytes, or "zero length")
Server: <length><messag e>
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.co m>
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.ReadI nt16();
If stringLength > 0 Then
Dim stringData() As Byte = binReader.ReadB ytes(stringLeng th)
Dim msg As String = Encoding.SomeEn coding.GetStrin g(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
2484
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 statement. This resembles what I am doing: <form name="frm1"> Step 1 - Choose a File: <select name="selA" OnChange="makeinputbox;" width="200" style="width:200"> <option value="">Date of Birth</option> <option value="MASTER">Age</option>
6
9744
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 10:24:48 AM run() error: System.IO.IOException: The specified network password is not correct. at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare...
2
3575
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*> (request->GetResponse()); // Gets the stream associated with the response. Stream* receiveStream = response->GetResponseStream(); int n = receiveStream->Read(responseArray, 0, responseString->Length);
14
5850
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 want to store data like a database: ---------------------------------- Custumer:
3
7668
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 of file structure. The obvious solution would be to use XML file processing however that is not an option as I have a third party vendor that is not capable of processing XML files.
5
13305
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 number of search folders using the standard dialog control. There shouldn't be an issue with the search folder being on a local drive or a network drive. But if you don't have Read access to the folder, there's trouble. The user can see the...
2
272
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 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")
4
4196
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 everything using XmlRootAttribute, XmlTypeAttribute, XmlSerializerNamespacesAttribute, etc, and nothing will serialize to or deserialize from this root element format.
1
2136
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 declaration is as below. The problem is in "struct _WLAN_AVAILABLE_NETWORK_LIST", it declared "WLAN_AVAILABLE_NETWORK Network;". I think which means a Network array of struct WLAN_AVAILABLE_NETWORK.
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.