【发布时间】:2011-09-01 17:09:21
【问题描述】:
我有这个代码:
public static List<ReplicableObject> ParseStreamForObjects(Stream stream)
{
List<ReplicableObject> result = new List<ReplicableObject>();
while (true)
{
// HERE I want to check that there's at least four bytes left in the stream
BinaryReader br = new BinaryReader(stream);
int length = br.ReadInt32();
// HERE I want to check that there's enough bytes left in the stream
byte[] bytes = br.ReadBytes(length);
MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
result.Add((ReplicableObject) Formatter.Deserialize(ms));
ms.Close();
br.Close();
}
return result;
}
不幸的是,流对象总是一个 TCP 流,这意味着没有查找操作。那么我如何检查以确保我没有过度运行我放置 // HERE cmets 的流?
【问题讨论】:
-
您是否可以控制通过网络发送的内容?如果是,那么你可以简单地在另一边写下你在序列化你的对象时发送了多少字节......
-
是的,我有,而且我有(见'int length = br.ReadInt32()'这一行)。但现在我得到一个新的错误。我应该为它提出一个新问题吗?
-
@Motig - 是的,如果是新问题,您应该创建一个新问题
标签: c# serialization tcp stream