what is the most efficient way to move data across network? And st

  • Thread starter Thread starter chrisben
  • Start date Start date
C

chrisben

Hi, I would to send data as the most efficient way, as compact and as fast as
possible. So I think any serialization is out of picture.
A fundamental way is to translate all my fields in the class as bytes and
pass it, then reconstruct it on other side. This may also save me a lot of
hassle if passing between windows and linux, c# and c++.

So i try to use Buffer.BlockCopy to create byte array, pass it to socket.
use BitConverter to read them back. I like to use fixed size array for
efficient parsing at the client side. The only problem I run into is string

assuming I have one field called
string ID; I know ID has variable lengh but less than 6. So I like to pass
take 6 bytes of the array buffer and other side will take it and convert it
back. The problem is that I cannot find a proper method in C# to do it. Seems
I have to define the exact length to pass the array in C#. is that possible
to pass a variable length string to a fixed size byte array and convert it
back as string?
thanks
 
I'm gonna post this.........let me preface this as "some ideas"...........it
might be off base for what your asking.............But here goes:


But check out this Serializer combined with this endpoint type if you're
interested in WCF.

NetDataContractSerializer
"endpoint address=net.tcp"

I googled and found this example:
http://bertcraven.spaces.live.com/blog/cns!61EA3F98C8957A87!306.entry?sa=458189917


http://blogs.msdn.com/sowmy/archive/2006/06/06/all-about-knowntypes.aspx
This is a good WCF basic-understanding thing.

Keep in mind that you could write your own serializer.........you not tied
to just the out of the box ones.
(NetDataContractSerializer,DataContractSerializer, and the XmlSerializer)
http://www.lmgtfy.com/?q=wcf+custom+serialization

I've always used one of these with WCF.
NetDataContractSerializer
DataContractSerializer

But if you want small, where you can share the types, and size of the
serialization is a priority, then NetDataContractSerializer combined with
tcp endpoints.

This might be of small interest as well:
http://www.pluralsight.com/community/blogs/aaron/archive/2008/05/13/50934.aspx
 
chrisben said:
Hi, I would to send data as the most efficient way, as compact and as fast
as
possible. So I think any serialization is out of picture.
A fundamental way is to translate all my fields in the class as bytes and
pass it, then reconstruct it on other side. This may also save me a lot of
hassle if passing between windows and linux, c# and c++.

Maybe json with numerics rather than field names.
With soap or http you can pass pretty much what you like.

WCF and tcp are efficient and you can avoid serializing except I'm not sure
you can handle the data in whatever linuz process you have intimated might
possibly be involved somehow.
 
chrisben said:
Hi, I would to send data as the most efficient way, as compact and as fast as
possible. So I think any serialization is out of picture.
A fundamental way is to translate all my fields in the class as bytes and
pass it, then reconstruct it on other side. This may also save me a lot of
hassle if passing between windows and linux, c# and c++.

You are correct. For efficiency of _transmission_ as well as
interoperability, you can't beat conversion of data types to raw bytes
according to a predefined, compact protocol and transmitting in that form.
So i try to use Buffer.BlockCopy to create byte array, pass it to socket.
use BitConverter to read them back. I like to use fixed size array for
efficient parsing at the client side. The only problem I run into is string

assuming I have one field called
string ID; I know ID has variable lengh but less than 6. So I like to pass
take 6 bytes of the array buffer and other side will take it and convert it
back. The problem is that I cannot find a proper method in C# to do it. Seems
I have to define the exact length to pass the array in C#. is that possible
to pass a variable length string to a fixed size byte array and convert it
back as string?

The first thing you need to do is understand character encoding.
Characters are not bytes in C#. They are text encoded as UTF-16, so
each character takes up 2 bytes (4 bytes for surrogate pairs).

You can use the System.Text.Encoding class to convert between string and
byte arrays, using whatever encoding for the bytes you want. For
example, UTF-8 (which supports all Unicode characters, but can still
encode the first 128 character values in a single byte like ASCII does),
or ASCII (which is exactly one byte per character, but which can
represent only a very small subset of the characters that are possible
in .NET).

As far as dealing with the length of the string, you can use a
fixed-sized buffer if you want, but note that you'll still need some way
of terminating strings that don't fill the buffer (e.g. zero byte
terminator), and then copying the fixed-sized buffer back to a
correctly-sized buffer after receipt of the data, so that the Encoding
class doesn't return (e.g.) a string with embedded nulls.

Pete
 
There it is:

//Quote//
Extend the XmlObjectSerializer to create your own serializer to serialize
and deserialize objects.



System..::.Object
System.Runtime.Serialization..::.XmlObjectSerializer
System.Runtime.Serialization..::.DataContractSerializer
System.Runtime.Serialization.Json..::.DataContractJsonSerializer
System.Runtime.Serialization..::.NetDataContractSerializer


http://msdn.microsoft.com/en-us/library/system.runtime.serialization.xmlobjectserializer.aspx


So maybe you can write up that sweet custom serializer that PeterD and you
brought up....and post the code!

..........
 
Back
Top