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
}