【问题标题】:C# send/receive Collections using protobuf-net + sockets exampleC# 使用 protobuf-net + 套接字示例发送/接收集合
【发布时间】:2013-05-21 11:50:11
【问题描述】:

我正在寻找如何使用 protobuf-net 序列化和套接字连接在两台机器之间传输集合的完整示例。我成功找到的只是代码的单独部分,没有人不能向像我这样的菜鸟解释如何组合所有这些东西。在 .proto 文件之后:

using System;
using ProtoBuf;

namespace Proto.Transaction
{    
    [Serializable]
    [ProtoContract]
    public class Transaction
    {
        [ProtoMember(1)]
        public int Type { get; set; }

        [ProtoMember(2)]
        public int AgentID { get; set; }

        [ProtoMember(3)]
        public string TransactionName { get; set; }

        [ProtoMember(4)]
        public string ScriptName { get; set; }

        [ProtoMember(5)]
        public DateTime StartTime { get; set; }

        [ProtoMember(6)]
        public double TransactionLength { get; set; }
    }
}

最后,我必须将其作为 Observable Collection 接收并将其设置为 DataSet(以便将其保存为 XML 并在 DataGrid 中呈现)。我将不胜感激任何帮助和/或指向网络中适当材料的链接。 问候

【问题讨论】:

  • 归根结底,这里的 protobuf-net 没有什么不同。任何向您展示如何通过套接字发送 byte[] 消息的示例就足够了。但是,我很确定源代码中有一个套接字项目示例 - 稍后会检查(而不是在 PC 上)
  • 嗨,马克,感谢您的快速回复。

标签: c# communication protocol-buffers protobuf-net


【解决方案1】:

最基本的层面,您可以查看QuickStart/3 Sockets.cs 下的示例。这里的关键点是如果您在原始套接字级别工作,您将需要在消息之间添加某种分区(因为 TCP 只是一个流)。该示例通过使用*WithLengthPrefix 方法来完成此操作。

但是,在许多情况下,您可能能够使用已经包含消息分区的预滚动 RPC / 消息传递实现;在这种情况下,您不需要使用*WithLengthPrefix 方法(尽管欢迎您这样做)。

【讨论】:

    猜你喜欢
    • 2013-04-01
    • 2017-04-24
    • 2013-11-13
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 2021-07-08
    • 2016-07-28
    • 1970-01-01
    相关资源
    最近更新 更多