Hello, I am trying to send an object over a socket using C#. I originally used a memorystream and binaryformatter, but to do this I need to include a reference from program1 to program2. I don't want to have a reference, so I just want to be able to transform an object to a byte[] but this does not seem to work either when casting it on the otherside of the wire back to object. So for instance

Object obj = MyClassObj;
byte[] bytes = (byte[])obj;

(send over wire)

Program1 -> Program2. It seems as though it does not transfer the byte array correctly, I am guessing because I didn't serialize it correctly. Does anyone know how I can create a byte array to send over a wire without using a memorystream/binaryformatter, or how I can do it without having to reference the other application?

Thank you!

Converting your object to bytes or using the memorystream and binaryformatter seem the same to me.

Consider this. If I was to take down my house brick by brick and then give you the bricks you would need plans on how to put it back together.

The same is true of objects being passed from program to program. When you pass an object from one process to another you will always need to include a reference to the objects library.

Instead of using low level sockets, have you thought about using Channels.
Look at the System.Runtime.Remoting.Channels namespace. There are objects there that suport RPC.

commented: for taking down your house brick by brick. +2

Nick,
Thank you for your reply. And what you said makes sense, but from a native C++ side, if I take those bricks off one by one and put them back one by one in the same order i.e. the order they arrive on the network stack and then cast it to the correct object type (would obviously need the class defined the exact same way in the receiving application), I would think there should be a way to do it. I have never heard of the remoting channels, thanks for the heads up. From what I have seen so far though, it only supports TCP/HTTP no way to multicast.

I re-read your original post and realised you do not need to pass a reference (which is what channels do by the way) so I had another think.

Because C# does not allow direct access to the raw implementation (that would be unmanaged), the quick and dirty way to do it like C++ is to add a method to extract the data to a byte array and then at the remote end have a contructor that takes the raw byte data to form the object.

However, it is possible to do this using the BinaryFormater.
If you stream the binaryformater output to a file you will see that it contains fully qualified type names for the objects (this is to maintain strong typing).

The BinaryFormater.Binder property provides a mechanism to override this typing.
Set the Binder property to a class derived from SerializationBinder. This allows you to override the requested type with one from a different library.

SerializationBinder Class Help from MSDN

You could always just binary serialize the object, write it to the stream, then deserialize the object from a buffer. Or even xml serialize the object to a string, then send the string as an array of bytes, and deserialize the string from the buffer back into the original object. Then all your program really needs is a common class to represent the object, or more simply. Just a common interface definition.

You could always just binary serialize the object, write it to the stream, then deserialize the object from a buffer. Or even xml serialize the object to a string, then send the string as an array of bytes, and deserialize the string from the buffer back into the original object. Then all your program really needs is a common class to represent the object, or more simply. Just a common interface definition.

Hi Drake, this is actually what I was trying to do but was unable to find any examples and didn't see anything in the SDK documentation to "Binary serialize the object". Do you have any links of examples you can post?

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.