Search code examples
c#imagesocketsudpbinary-deserialization

No assembly ID for object type 'ImagePacket'.De Serialization Exception Caught C#


I am Sending Serialized large Image Object over UDP Socket.When I write all received bytes in Memory stream and pass the memory stream object for deserialization it throws an exception No assembly ID for object type 'ImagePacket'.

Receiver End Code:

                 ImageStream = new MemoryStream();

                while (AccumulatingBytes <= TotalSizeOfComplexObject)
                 {

                  byte[] Recievedbytes = UdpListener.Receive(ref RemoteEndPoint);

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

                   AccumulatingBytes += Recievedbytes.Length;
                 } 


                  ImageStream.Position = 0;

                    imagecontainer = (ImageContainer)bformater.Deserialize(ImageStream);//Here the Code Segment Breaks and Exception thrown

Solution

  • I suspect the problem here is simply: you are using UDP like it is TCP. UDP is packet based, but a: doesn't guarantee that the packets will arrive in order, and b: doesn't guarantee that packets won't be dropped or duplicated.

    I fully expect you have some out of order. If you are sending multiple messages, it is also possible some were dropped, and you've included a few from the next message.

    To use the network the way your code wants to use it: use TCP. Otherwise, the responsibility for making sense of out-of-order, dropped and duplicated packets is entirely yours. This could be, for example, by adding a sequence number to the packet, and keeping track of what has been received - re-ordering them as necessary, dropping duplicates, and re-requesting any that died en-route. Basically, re-writing everything that TCP adds! Unless you have a very specific scenario, there's a good chance that the TCP stack (with NIC and OS level support) will do a better job of this than you will.