Sending objects using UdpClient C #

I am currently testing visual studio 2010. I have created a client and server that will both connect through UdpClient.

I want to send an object from a client to a server. I have two methods for converting an object to bytes and converting it to an object. Now, when I test my application, I cannot convert it back to an object once received on the server

My server sees that the object is received and tries to convert it from bytes to the object, but this gives an error.

System.Runtime.Serialization.SerializationException was unhandled   Message=Unable to find assembly

This looks fine because both applications are in a different namespace ...

These are my methods for conversion; And the same thing on the client and server

public byte[] ToBytes() {
        using (MemoryStream stream = new MemoryStream()) {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, this);

            stream.Position = 0;

            byte[] byteRij = new byte[1024];

            stream.Read(byteRij, 0, (int)stream.Length);

            return byteRij;
        }
    }

    public static Datagram ToDatagram(byte[] rij) {
        using (MemoryStream stream = new MemoryStream()) {
            stream.Write(rij, 0, rij.Length);

            stream.Position = 0;

            BinaryFormatter formatter = new BinaryFormatter();
            return (Datagram)formatter.Deserialize(stream);
        }
    }

How can i solve this? thanks in advance

+5
3

, . , .

, UDP . , .

+3

BinaryFormatter . , . - :) .

protobuf-net (: ). OSS, goob protobuf BF. , , BinaryFormatter. , , ( ..).

:

[ProtoContract]
public class Foo {
    [ProtoMember(1)]
    public string X {get;set;}
    [ProtoMember(2)]
    public int Y {get;set;}
}

ProtoBuf.Serializer.Serialize(, ) .

, , , .

+3

. , .

: MyApp1.MyFoo.
MyFoo , MyApp2.MyFoo ( , ). , MyApp1.MyFoo, , , , MyApp2.MyFoo.

. . , . MyApp.Server MyApp.Client ;).

, .

+1

All Articles