[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Serializing / Deserializing binary data via a NetworkStream

Jonathan Jones

11/6/2008 3:41:00 PM

All,

I am writing some C# client / server stuff that interfaces with hardware via
some physical port (CAN or RS232). I am receiving messages via the physical
port and then I have a message class that parses the data and stuffs the
parsed data into a new message object so it is available to any clients that
want it. I understand how to use the BinaryFormatter class but my question
is regarding getting the bytes sent out via the NetworkStream and having it
deserialized properly on the other side.

So I create my BinaryFormatter and then call
BinaryFormatter.Serialize(stream, msg) where stream is the underlying
NetworkStream from the TcpListener and msg is my object. How does the client
know how much data to use to deserialize the object? Do I have to send some
sort of synch message that includes data length so it knows there is a
message coming and how long it is? If that's the case, that's fine. I
didn't know if there if there was something I was missing in my understanding.

Thanks!

Jonathan Jones
3 Answers

Jonathan Jones

11/6/2008 6:55:00 PM

0

Ok,

I did some examples and I have another question. Because I have a class
that wraps the TcpClient and NetworkStream stuff, I don't plan on writing
directly to the NetworkStream. I want to write to a MemoryStreamand then use
the ToArray() method to pass the returned byte array to my Write() method
(which uses NetworkStream.BeginWrite()).

As a test, I created a simple 6 byte array and passed it to the
BinaryFormatter.Serialize(stream, object) as the object to be serialized.
The resulting MemoryStream was 35 bytes long! I know this is a poor example
because I could have just sent the 6-bytes via a direct Write() call, but is
that kind of bloat normal? And more importantly, does it save time if you are
sending 5 - 6 times the amount of data you would normally be sending.

Jonathan

Peter Duniho

11/6/2008 7:33:00 PM

0

On Thu, 06 Nov 2008 10:55:04 -0800, Jonathan Jones
<JonathanJones@discussions.microsoft.com> wrote:

> [...]
> As a test, I created a simple 6 byte array and passed it to the
> BinaryFormatter.Serialize(stream, object) as the object to be serialized.
> The resulting MemoryStream was 35 bytes long! I know this is a poor
> example
> because I could have just sent the 6-bytes via a direct Write() call,
> but is
> that kind of bloat normal? And more importantly, does it save time if
> you are
> sending 5 - 6 times the amount of data you would normally be sending.

Does it save what kind of time?

Obviously if you are sending five times as much data as was originally
contained in your data structure, then it will take five times as long to
send the data. It's trivial to show that's not "saving time" in terms of
data transmission.

But it certainly saves time in terms of development. Whether it saves
enough time to be worth it to you, I can't say. Sending data across a
network isn't rocket science. But it does take _some_ time to implement
your own serialize/deserialize logic. And if this is code you're going to
have to revisit on a regular basis -- if your own message objects might
change, you have to support different data sources (hardware), etc. --
then it might be worthwhile to just use the built-in stuff rather than
maintaining your own serialization.

Note that for larger amounts of data (say, 1K or more), you may be able to
use the GzipStream class to compress the data. Yes, it seems kind of
silly to bloat the data, and then try to compress it back down again. But
again, if that's the more maintainable approach, maybe it's good enough
for your purposes.

After all, you're already doing something silly like writing to a
MemoryStream and then passing the generated byte array to a
NetworkStream. :)

Pete

MikeHansen

11/7/2008 8:50:00 PM

0

I agree. There are a lot simpler ways of doing binary serialization for streams of data and standard data types. The BitConverter class has a few methods to make this real easy (see <a href="http://blog.hansentech.com/mhblog_2008_11_7.html"... Data Serialization</a>) for communications where the protocol doesn't need to change with every release.