在解析完成之前遇到流结束通过 UDP 套接字发送大图像
我正在通过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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些明显的观察结果可能会有所帮助...
MemoryStream
?这是最直接的问题AccumulatedBytes += Receivedbytes.Length;
另外;如果您不自己处理错误和丢失数据,请使用 TCP。
所以类似:
然后在反序列化之前设置
ImageStream.Position = 0
。您还应该检查UdpListener
是否未报告 EOF。Some obvious observations that may help...
MemoryStream
each 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 = 0
before deserializing. You should also probably check thatUdpListener
isn't reporting EOFs.你一直在0处写字节流吗?
也许您需要而不是零位变量 AccumulatedBytes ?
Did you write bytes stream at 0 place all the time?
Maybe you need instead of zero place variable AccumulatingBytes ?