0

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
}
JJ Binks
  • 13
  • 1
  • 7
  • 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 Aug 12 '18 at 03:39
  • I have modified the original post to add what you are asking about. – JJ Binks Aug 12 '18 at 03:41
  • 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 Aug 12 '18 at 03:48
  • 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 Aug 12 '18 at 04:13
  • Is there an equivalent method for serializing where it is not dependent on the assembly-class culture? – JJ Binks Aug 12 '18 at 04:26
  • Can you share the full `ToString()` output of the exception including the exception type, message and inner exception as well as the traceback? Currently you're only sharing the traceback. Also, for a general overview of issues using `BinaryFormatter` see [What are the deficiencies of the built-in BinaryFormatter based .Net serialization?](https://stackoverflow.com/q/703073/3744182), perhaps some apply here. – dbc Aug 15 '18 at 07:36

1 Answers1

0

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.

Mojtaba Tajik
  • 1,725
  • 16
  • 34
  • Thanks for the response! However, I could not seem to make it work even with that article. I kept getting "No assembly ID for object type 'Matchmaker.Net.Enums.NetObjectType'." error. I am switching to Json and just going to serialize using ASCII encoding. If anyone can figure this problem out, I'd love to switch back. – JJ Binks Aug 12 '18 at 12:06