Shredder2500
Shredder2500

Reputation: 1085

why is this throwing a end of stream encountered before parsing was completed?

Im not sure why this is throwing the error End of stream encountered before parsing was completed. I looked at this link but nothing on there fixed the problem in my case. I used break points to find the exact point it throws the error witch is

       Console.WriteLine("Message from client ");

Thanks in advance

   if (stream.DataAvailable)
   {
       byte[] buffer = new byte[10024];
       int byteCount = stream.Read(buffer, 0, buffer.Length);
       byte[] inBytes = new byte[byteCount];
       for (int counter = 0; counter < byteCount; counter++)
           inBytes[counter] = buffer[counter];
       BinaryFormatter formatter = new BinaryFormatter();
       MemoryStream memory = new MemoryStream();
       memory.Write(inBytes,0,inBytes.Length);
       object message = (object)formatter.Deserialize(memory);
       Console.WriteLine("Message from client ");
       memory.Close();
   }

Edit* this was my bad nothing was wrong with the actual code here i made the mistake on the client end. i used Encoding.ASCII Instead of a binaryformatter.

Upvotes: 0

Views: 3242

Answers (2)

Shredder2500
Shredder2500

Reputation: 1085

this was my bad nothing was wrong with the actual code here i made the mistake on the client end. i used Encoding.ASCII Instead of a binaryformatter.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500155

Well, you're assuming that a single call to stream.Read is going to read all of the data. That's almost always a mistake, especially if it's a network stream.

Is there any reason you're not just calling formatter.Deserialize(stream) in the first place?

Upvotes: 5

Related Questions