I've been having the same exception for the past three days now, and it's starting to drive me mad.
I am using TCP to send a serialized byte array over the Interwebz, and I then deserialize it. However, every second time deserializing the array it gives me an exception:
"Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization."
Both the project for the server, and the project for the client use the same referenced DLL(Project) as marked the solution for most of the cases that this exception occurs. I also have resetted the stream position which has also been marked as an answer. Besides that I even checked if the byte array changed over time and it has not.
Now before answering this please take note that this worked a few projects ago, and there is no difference to any of the methods I created, and also I even made a more complex class to serialize with a less chance of getting an error.
The object I'm attempting to serialize:
[Serializable]
public class PacketAudioUpdate : ISerializable
{
public string SoundBytes;
public PacketAudioUpdate()
{
}
protected PacketAudioUpdate(SerializationInfo info, StreamingContext context)
{
SoundBytes = info.GetString("soundbytes");
}
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter =true)]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("soundbytes", SoundBytes);
}
}
Serialization/Deserialization methods:
public static byte[] ObjectToByteArray(Object obj)
{
MemoryStream fs = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, obj);
byte[] rval = fs.ToArray();
fs.Close();
return rval;
}
public static object ByteArrayToObject(Byte[] Buffer)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream(Buffer);
stream.Position = 0;
object rval = formatter.Deserialize(stream);
stream.Close();
return rval;
}
Exception log:
> Protocol.dll!Protocol.NetClient.HandleClient() Line 228 C#
>
if (OnPacket != null)
OnPacket(this, new OnPacketEvent(Packet, Client));
>
void ChatClientClient_OnPacket(object sender, NetClient.OnPacketEvent e)
{
object parObject = Conversion.ByteArrayToObject(e.Packet);
if (OnPackageReceived != null)
OnPackageReceived(this, new OnPackageReceivedEvent(parObject));
}
Edit:
This is how I send information:
public void SendPacket(byte[] Packet)
{
Client.GetStream().Write(BitConverter.GetBytes(Packet.Length), 0, BitConverter.GetBytes(Packet.Length).Length);
Client.GetStream().Write(Packet, 0, Packet.Length);
}
This is how I receive information:
public void HandleClient()
{
int BytesRead = 0;
int BufferSize = 0;
Stream = Client.GetStream();
datalength = new byte[4];
while (true)
{
try
{
Stream.Read(datalength, 0, datalength.Length);
BufferSize = BitConverter.ToInt32(datalength, 0);
}
catch { break; }
if (BufferSize != 0)
{
BytesRead = 0;
Packet = new byte[BufferSize];
try
{
BytesRead = Stream.Read(Packet, 0, BufferSize);
}
catch { break; }
if (BytesRead == 0)
{
break;
}
if (OnPacket != null)
OnPacket(this, new OnPacketEvent(Packet, Client));
}
else
{
break;
}
}
StopClient();
}