1
private byte[] ToByteArray(Message msg)
    {
       if(msg == null)
        {
            return null;
        }
        BinaryFormatter bf = new BinaryFormatter();
        using(MemoryStream ms = new MemoryStream())
        {
            bf.Serialize(ms, msg);
            return ms.ToArray();
        }
    }
    private Message ToMessageObject(Byte[] bytes)
    {
        if(bytes == null)
        {
            return null;
        }
        BinaryFormatter bf = new BinaryFormatter();
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            ms.Write(bytes, 0, bytes.Length);
            ms.Position = 0;
            Message msg = (Message)bf.Deserialize(ms);
            return msg;
        }
    }

I am using the two above methods for serialization and deserialization, but it keeps on throwing the error during deserializaion.

system.runtime.serialization.serializationexception: 'end of stream encountered before parsing was completed.'

My message class has the "Serializable" attribute. One of the things I noticed is when the text is small, like two words, it deserializes fine but when it contains a lot of characters, close to 100, that's when I get that error. I have been checking other solutions()frome here and other places to this problem but none seems to work for me.

  • Where is the the input msg coming from? Check the source msg for number of bytes and then compare with received number of bytes. You are trying to parse the data before all the data was received. Deserialization requires all the bytes before processing. – jdweng Jun 07 '20 at 12:09
  • @jdweng, So, I am trying to implement a client and server application that communicates via tcp, so the message comes from another or same instance of the client. Thank you, I was able to figure out the problem, I had set the size of the byte array to 256 in the server side, so everything greater than 256 threw the error. so I'm wondering if there's any way to dynamically tell the length of the bytes being sent – abdulmueez emiola Jun 07 '20 at 12:30
  • 1
    Does this answer your question? [End of Stream encountered before parsing was completed?](https://stackoverflow.com/questions/306596/end-of-stream-encountered-before-parsing-was-completed) – Józef Podlecki Jun 07 '20 at 12:39
  • @JózefPodlecki, Not at all, but I already figured the problem out, I set the size of my byte array to a constant, Thanks for your help – abdulmueez emiola Jun 07 '20 at 12:50
  • You could write an answer for that. It's not obvious – Józef Podlecki Jun 07 '20 at 12:51
  • @JózefPodlecki, I have done that, Thank you. – abdulmueez emiola Jun 07 '20 at 13:01
  • Binary data you have to add a byte count when transmitting, Then on receive read the byte count and then continue reading until all the bytes are received. – jdweng Jun 07 '20 at 13:55

1 Answers1

0

From the comments, I was able to figure out that the problem was caused by the initialization of the byte array,

var responseStream = client.GetStream()
var bytes = new byte[1024];
if (responseStream.DataAvailable)
{
   await responseStream.ReadAsync(bytes, 0, bytes.Length);
   var responseMessage =ToMessageObject(bytes);
   messages.Add(responseMessage);
}

Initially, I set the bytes array length to 256. The error was thrown anytime I had to deserialize a byte array of length greater than 256. So, I am wondering if it's possible to use a dynamic array when reading from a stream.