ZMQ的C#protobuf反序列化错误,写入类型无效

时间:2018-12-18 11:11:52

标签: c# deserialization zeromq protobuf-net

我有两个程序。与服务器创建     DiscoverServerMessage 并通过protobuf进行序列化,然后通过ZMQ发送。在客户端反序列化消息但出错:

Ivalid wire-type; this usually means you have over-written a file without truncating or setting the length.

我在dll项目中有以下课程。

[ProtoContract]
public class DiscoverServerMessage 
{
    [ProtoMember(1)]
    public int Port { get; set; }
    [ProtoMember(2)]
    public string IP { get; set; }
    [ProtoMember(3)]
    public string ServerName { get; set; }
    public DiscoverServerMessage() { }
}

在Server Project中具有以下代码:

public class Server
{
    static void Main(string[] args)
    {
        ZContext ctx = new ZContext();
        ZSocket broadcastSrocket = new ZSocket(ctx, ZSocketType.PUB);
        broadcastSrocket.Connect("tcp://127.0.0.1:2666");
        ZFrame zfr;
        DiscoverServerMessage message = new DiscoverServerMessage() { Port = 2667 , ServerName = "My" };
        message.IP = "127.0.0.1";
        using (MemoryStream str = new MemoryStream())
        {
            Serializer.SerializeWithLengthPrefix(str,message,PrefixStyle.Fixed32);
            zfr = new ZFrame(str.ToArray());
        }
        broadcastSrocket.Send(zfr);
    }
}

在客户端程序中:

public class Server
{
    static void Main(string[] args)
    {
        ZContext ctx = new ZContext();
        ZSocket subSocket = new ZSocket(ctx, ZSocketType.SUB);
        subSocket.Connect("tcp://127.0.0.1:2666");
        subSocket.SubscribeAll();
        ZError err;
        ZFrame zfr = subSocket.ReceiveFrame(out err);
        DiscoverServerMessage message;
        using (MemoryStream stream = new MemoryStream(zfr.Read()))
        {
            message = Serializer.DeserializeWithLengthPrefix<DiscoverServerMessage >(stream,PrefixStyle.Fixed32);
        }
    }
}

我的错误是什么?

1 个答案:

答案 0 :(得分:0)

要做的第一件事是忘记有关序列化的部分,只需检查发送的内容是否是收到的内容即可,因此-获得str.ToArray()是(服务器),并将其与zfr.Read()是什么(客户端)进行比较。如果两个有效载荷不相同:所有其他投注均关闭。最简单的方法通常是通过十六进制,因此:

(服务器):

byte[] blob = str.ToArray();
string hex = BitConverter.ToString(blob); // <== get a copy of this somehow
zfr = new ZFrame(blob);
...

(客户)

byte[] blob = zfr.Read();
string hex = BitConverter.ToString(blob); // <== get a copy of this somehow
using (MemoryStream stream = new MemoryStream(blob))
...

然后检查一下:两个十六进制序列是否相同?

注意:由于您使用的是成帧协议(ZFrame),因此这里实际上不需要长度前缀,但是...应该不会受到伤害。看起来 就像您在此处使用的是适当的代码一样-以下代码(受代码影响)可以正常工作:

static class Program
{
    static void Main()
    {
        byte[] blob;

        // taken from server code
        using (MemoryStream str = new MemoryStream())
        {
            DiscoverServerMessage message = new DiscoverServerMessage() { Port = 2667, ServerName = "My" };
            message.IP = "127.0.0.1";

            Serializer.SerializeWithLengthPrefix(str, message, PrefixStyle.Fixed32);
            blob = str.ToArray();
        }
        System.Console.WriteLine(BitConverter.ToString(blob));

        // taken from client code
        using (MemoryStream stream = new MemoryStream(blob))
        {
            DiscoverServerMessage message;
            message = Serializer.DeserializeWithLengthPrefix<DiscoverServerMessage>(stream, PrefixStyle.Fixed32);
            Console.WriteLine(message.Port);
            Console.WriteLine(message.ServerName);
            Console.WriteLine(message.IP);
        }

    }
}

这里的输出是:

12-00-00-00-08-EB-14-12-09-31-32-37-2E-30-2E-30-2E-31-1A-02-4D-79
2667
My
127.0.0.1

如果仅使用Serialize / Deserialize(没有长度前缀)来做同样的事情,我们将得到(可预测):

08-EB-14-12-09-31-32-37-2E-30-2E-30-2E-31-1A-02-4D-79
2667
My
127.0.0.1

因此,只要客户端和服务器同意(它们出现在您的代码中),它应该在有或没有长度前缀的情况下都可以工作。