Serialize your object to a byte array. Send the client an int
containing the length of the byte array, followed by the byte array.
The client reads the int from the network stream, and then reads
number of bytes specified by the int into a byte array. DeSerialize
the byte array into an object and away you go.

Be sure to replace the default automagic versioning, e.g. [assembly:
AssemblyVersion("1.0.*")],
in your AssemblyInfo file for the objects you are sending over the
socket with a fully specified version number to all 4 places. e.g.
"1.2.3.456"

Here's a simple class you can use to serialize from/to a byte array.
This is from some code I wrote on one of the beta cycles of .Net, and
as such you'll no doubt need to refactor it a bit to make it
production quality, such as adding try/finally blocks around the
memory streams to ensure they get closed.

public class Serializer {
        public Serializer(){}

        public byte[] Serialize(object o){
                MemoryStream s = new MemoryStream();
                BinaryFormatter b = new BinaryFormatter();

                b.Serialize(s, o);
                if (s.Length > int.MaxValue)
                        throw new ArgumentException("Serialized object is larger than 
can
fit into byte array");

                byte[] buffer = new Byte[s.Length];
                s.Seek(0, SeekOrigin.Begin);
                s.Read(buffer, 0, (int)s.Length);
                s.Close();
                return buffer;
        }

        public object DeSerialize(byte[] bytes){
                MemoryStream s = new MemoryStream(bytes);
                BinaryFormatter b = new BinaryFormatter();
                object o = b.Deserialize(s);
                s.Close();
                return o;
        }
}


Here's the method on the client side that reads bytes from a stream
and returns an object. The type of reader used is an instance of a
BinaryReader, and serializer is an instance of the Serializer above.

public object Read(Stream stream) {
        int length = reader.ReadInt32();
        byte[] data = reader.ReadBytes(length);
        return serializer.DeSerialize(data);
}

Keep Smilin'
Ed Stegman



-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] Behalf Of dotnetminer
Sent: Tuesday, October 28, 2003 9:34 PM
To: [EMAIL PROTECTED]
Subject: Serialization Problem


I am serializing an object and sending it through networkstream to
another
computer. However when i try to deserialize it, it is giving me a
error "BinaryFormatter Version incompatibility. Expected Version 1.0
Recieved version 1566270836.0"

I am using socket programming and not remoting. I am creating the
application on a stand alone machine having only one version of .NET
framework.
How can i resolve this error?

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
NEW! ASP.NET courses you may be interested in:

2 Days of ASP.NET, 29 Sept 2003, in Redmond
http://www.develop.com/courses/2daspdotnet

Guerrilla ASP.NET, 13 Oct 2003, in Boston
http://www.develop.com/courses/gaspdotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to