通过 TCP 的 C# 反序列化

发布于 2024-12-02 22:07:11 字数 1545 浏览 0 评论 0原文

我现在遇到反序列化通过 TCP 发送的对象的问题。

反序列化时,我得到以下 SerializationException (代码如下):

附加信息:二进制流“0”不包含有效的 二进制标头。可能的原因是无效的流或对象版本 序列化和反序列化之间的变化。

序列化代码:

public static void SerializeRO(Stream stream, ReplicableObject ro) {
            MemoryStream serializedObjectStream = new MemoryStream();
            Formatter.Serialize(serializedObjectStream, ro);
            BinaryWriter bw = new BinaryWriter(stream);
            bw.Write(serializedObjectStream.Length);
            serializedObjectStream.WriteTo(stream);
            serializedObjectStream.Close();
            bw.Close();
        }

反序列化代码:

public static List<ReplicableObject> ParseStreamForObjects(Stream stream) {
        List<ReplicableObject> result = new List<ReplicableObject>();
        while (true) {
            if (!(stream as NetworkStream).DataAvailable) break;
            BinaryReader br = new BinaryReader(stream);
            int length = br.ReadInt32();
            byte[] bytes = br.ReadBytes(length);
            MemoryStream ms = new MemoryStream(bytes);
            ms.Position = 0;
                            // ERROR OCCURS ON THE LINE BELOW
            result.Add((ReplicableObject) Formatter.Deserialize(ms));
            ms.Close();
            br.Close();
        }
        return result;
    }

对象在运行时被序列化,所以我不认为这是版本控制问题。我是流媒体等新手,所以我可能错过了一些明显的东西。

我想提出我认为可能的建议,但我真的被困住了。 :)

谢谢。

I have now an issue with deserializing an object sent over TCP.

When deserializing, I get the following SerializationException (code below):

Additional information: Binary stream '0' does not contain a valid
BinaryHeader. Possible causes are invalid stream or object version
change between serialization and deserialization.

Serialization code:

public static void SerializeRO(Stream stream, ReplicableObject ro) {
            MemoryStream serializedObjectStream = new MemoryStream();
            Formatter.Serialize(serializedObjectStream, ro);
            BinaryWriter bw = new BinaryWriter(stream);
            bw.Write(serializedObjectStream.Length);
            serializedObjectStream.WriteTo(stream);
            serializedObjectStream.Close();
            bw.Close();
        }

Deserialization code:

public static List<ReplicableObject> ParseStreamForObjects(Stream stream) {
        List<ReplicableObject> result = new List<ReplicableObject>();
        while (true) {
            if (!(stream as NetworkStream).DataAvailable) break;
            BinaryReader br = new BinaryReader(stream);
            int length = br.ReadInt32();
            byte[] bytes = br.ReadBytes(length);
            MemoryStream ms = new MemoryStream(bytes);
            ms.Position = 0;
                            // ERROR OCCURS ON THE LINE BELOW
            result.Add((ReplicableObject) Formatter.Deserialize(ms));
            ms.Close();
            br.Close();
        }
        return result;
    }

The objects are being serialized at runtime, so I don't think it's a versioning issue. I am new to streaming etc, so I may have missed something obvious.

I'd like to suggest what I think it could be, but I'm really stuck. :)

Thanks.

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

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

发布评论

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

评论(1

冷了相思 2024-12-09 22:07:11

serializedObjectStream.Length 是一个long

您正在向网络写入 64 位值,但尝试将其作为 32 位 int 读取。

serializedObjectStream.Length is a long.

You're writing a 64-bit value to the network, but you're trying to read it as a 32-bit int.

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