Samie
Samie

Reputation: 83

End of Stream encountered before parsing was completed thrown Sending Large Images over UDP Socket

I am Sending Jpeg Encoded Images as Serialized Complex object over UDP Socket..As UDP Datagram Support max. Length of 52KB to 54KB,I m Writing the arrived Datagrams to memory stream that I could DeSerialize it at once.

Receiver End Code:

while (AccumulatingBytes <= TotalSizeOfComplexObject)//Size of Complex Object after Serialization which I get through TCP//
{
    byte[] Recievedbytes = UdpListener.Receive(ref RemoteEndPoint);//I m Sending fixed size of 204 NUMBER OF BYTES

    ImageStream = new MemoryStream();
    ImageStream.Position = (long)AccumulatingBytes;
    ImageStream.Write(Recievedbytes, 0, Recievedbytes.Length);

    AccumulatingBytes += 204;
}

When I deSerialize this Memory Stream Exception is Thrown.

Upvotes: 1

Views: 822

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1064244

Some obvious observations that may help...

  • why a new MemoryStream each time in the loop? this is the most immediate problem
  • why not AccumulatingBytes += Receivedbytes.Length;

Also; if you aren't handling errors and missing data yourself, use TCP.

So something like:

ImageStream = new MemoryStream();
while (AccumulatingBytes <= TotalSizeOfComplexObject)
{
    byte[] Recievedbytes = UdpListener.Receive(ref RemoteEndPoint);

    ImageStream.Write(Recievedbytes, 0, Recievedbytes.Length);

    AccumulatingBytes += Recievedbytes.Length;
} 

then set ImageStream.Position = 0 before deserializing. You should also probably check that UdpListener isn't reporting EOFs.

Upvotes: 1

zzfima
zzfima

Reputation: 1565

Did you write bytes stream at 0 place all the time?

ImageStream.Write(Recievedbytes, 0, Recievedbytes.Length);

Maybe you need instead of zero place variable AccumulatingBytes ?

Upvotes: 0

Related Questions