0
votes

My program is serializing a class and is sending it over the network and being received on the other end. When the receiving server gets the byte array and tries to deserialize it, it causes an error. When I try to compare the data sent originally to newly generated data on the server (using the same functions on the same class, ex: trying to see if the same function on the server produces the same result), the result is completely different. How is this possible?

I cannot seem to figure this out.

public static byte[] ObjectToByteArray(object obj)
    {
        if (obj == null)
            return null;

        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }

    private static Object ByteArrayToObject(byte[] arrBytes)
    {
        MemoryStream memStream = new MemoryStream();
        BinaryFormatter binForm = new BinaryFormatter();
        memStream.Write(arrBytes, 0, arrBytes.Length);
        memStream.Seek(0, SeekOrigin.Begin);
        Object obj = binForm.Deserialize(memStream);
        return obj;
    }

And this is the class I am using for the serialization:

[Serializable]
public class NetworkObject
{
    public NetObjectType requestType;

    public NetworkObject(NetObjectType type)
    {
        requestType = type;
    }
}

EDIT: Here is the error thrown:

at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly() at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name) at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable) at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record) at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum) at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run() at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream) at Matchmaker.Net.Network.SocketManager.ByteArrayToObject(Byte[] arrBytes) in X:\Programming\VS\matchmake .net\matchmaker.net\matchmaker.net\Matchmaker\Network\SocketManager.cs:line 220 at Matchmaker.Net.Network.SocketManager.readAsyncBytes(IAsyncResult result) in X:\Programming\VS\matchmake .net\matchmaker.net\matchmaker.net\Matchmaker\Network\SocketManager.cs:line 126

Also, the contents of NetObjectType:

    public enum NetObjectType
{
    CLIENT_REQUEST_SERVER_LIST,
    CLIENT_SERVER_RESPONSE_GENERIC,
    CLIENT_SERVER_REGISTER_SERVER,
    CLIENT_SERVER_UNREGISTER_SERVER,
    CLIENT_SERVER_MODIFY_REGISTERED_SERVER,
    SERVER_SEND_MATCHMAKE
}
1
When you said "it causes an error", are you getting an actual exception? If so, what is it? Also, can you show the NetObjectType class? - mcbowes
I have modified the original post to add what you are asking about. - JJ Binks
And just to clarify: The data that I generate on the client and sent to the server is being sent in its entirety with no problem. What the problem seems to be is that when I try to serialize the same information (on the server), I get a completely different serialization array than what I got on the client. I believe this is what is causing the deserialization error but I don't understand why it would be behaving like this. - JJ Binks
That part of the exception is the where, you clipped the what which is that something serialized by A cannot be deserialized into B. Without a trick or two you can only deserialize to the same exact assembly-class-culture as what serialized it. use ProtoBuf - Ňɏssa Pøngjǣrdenlarp
Is there an equivalent method for serializing where it is not dependent on the assembly-class culture? - JJ Binks

1 Answers

0
votes

Implement your serializable class in common assembly which should use by client and server or use SerializationBinder to fix the issue caused by different namespace and etc.

This article is useful.