对象类型“ImagePacket”没有程序集 ID。捕获 C# 的序列化异常

发布于 2024-12-28 19:44:55 字数 749 浏览 0 评论 0原文

我正在通过 UDP 套接字发送序列化的大型图像对象。当我将所有收到的字节写入内存流并传递内存流对象进行反序列化时,它会抛出异常没有对象类型“ImagePacket”的程序集 ID '

接收端代码:

                 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2025-01-04 19:44:55

我怀疑这里的问题很简单:您像使用 TCP 一样使用 UDP。 UDP 是基于数据包的,但 a: 不保证数据包按顺序到达,b: 不保证数据包不会丢失或重复。

我完全希望你有一些故障。如果您发送多条消息,也可能有些消息已被删除,并且您已包含了下一条消息中的一些消息。

要按照代码想要的方式使用网络:使用 TCP。否则,理解无序、丢弃和重复数据包的责任完全由您承担。例如,这可以通过向数据包添加序列号,并跟踪已收到的内容 - 根据需要对它们重新排序,删除重复项,并重新请求任何在途中死亡的数据包。基本上,重写 TCP 添加的所有内容!除非您有非常具体的场景,否则 TCP 堆栈(具有 NIC 和操作系统级别支持)很有可能比您做得更好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文