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.
Some obvious observations that may help…
MemoryStreameach time in the loop? this is the most immediate problemAccumulatingBytes += Receivedbytes.Length;Also; if you aren’t handling errors and missing data yourself, use TCP.
So something like:
then set
ImageStream.Position = 0before deserializing. You should also probably check thatUdpListenerisn’t reporting EOFs.