Reputation: 1085
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
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
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