在解析完成之前遇到流结束通过 UDP 套接字发送大图像

发布于 2024-12-28 13:10:35 字数 658 浏览 0 评论 0原文

我正在通过UDP套接字发送Jpeg编码图像作为序列化复杂对象。作为UDP数据报支持最大。长度为52KB到54KB,我正在将到达的数据报写入内存流,以便我可以立即对其进行反序列化。

接收端代码:

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;
}

当我反序列化时,抛出此内存流异常

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.

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

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

发布评论

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

评论(2

无所的.畏惧 2025-01-04 13:10:35

一些明显的观察结果可能会有所帮助...

  • 为什么每次循环中都会有一个新的 MemoryStream ?这是最直接的问题
  • ,为什么不 AccumulatedBytes += Receivedbytes.Length;

另外;如果您不自己处理错误和丢失数据,请使用 TCP。

所以类似:

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

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

    AccumulatingBytes += Recievedbytes.Length;
} 

然后在反序列化之前设置ImageStream.Position = 0。您还应该检查 UdpListener 是否未报告 EOF。

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.

嘦怹 2025-01-04 13:10:35

你一直在0处写字节流吗?

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

也许您需要而不是零位变量 AccumulatedBytes ?

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 ?

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