在 boost 中序列化二进制数据失败并出现“无效签名”错误

     2023-02-21     171

关键词:

【中文标题】在 boost 中序列化二进制数据失败并出现“无效签名”错误【英文标题】:Serializing binary data in boost fails with `invalid signature' error 【发布时间】:2013-05-18 06:15:02 【问题描述】:

我很难找出使用 boost 序列化/asio 通过网络发送对象的正确方法。 message 类尽可能简单。它对 C++ 不友好,也不适合我的需求,我只是暂时保持简单以测试 asio/ser:

class message 
    friend class boost::serialization::access;
public:
    message()
    int type;
    int sender;
    int assignment;
    int nogood;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    
        ar & type;
        ar & sender;
        ar & assignment;
        ar & nogood;
    
;

在客户端,当代理决定发送消息时,通过它的 tcp 连接将其发送到服务器:

message m;
// do something to generate message
boost::asio::streambuf bufx;
std::ostream os( &bufx );
boost::archive::binary_oarchive ar( os );
ar & m;
boost::asio::write( socket, bufx);

服务器端代码:

boost::asio::streambuf bufx;
std::istream is(&bufx);
boost::archive::binary_iarchive ia(is);  // <--- Exception: invalid signature
size_t rcx = asio::read(socket,bufx);
message m;
ia >> m;

【问题讨论】:

【参考方案1】:

我遇到了同样的异常。

This official example 帮助了我。

如果您仍然遇到问题,请尝试此操作。

size_t n = sock.receive(bufs);

// received data is "committed" from output sequence to input sequence
b.commit(n);

std::istream is(&b);
std::string s;
is >> s;

就我而言,我使用 async_read。其实我修改了example。

  boost::asio::streambuf inbound_;
  boost::asio::streambuf::mutable_buffers_type bufs = inbound_.prepare(inbound_data_size);
  void (connection::*f)(
      const boost::system::error_code&, std::size_t,
      T&, boost::tuple<Handler>)
    = &connection::handle_read_data<T, Handler>;
  boost::asio::async_read(socket_, boost::asio::buffer(bufs), 
    boost::bind(f, this,
      boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, boost::ref(t), handler));

然后在处理程序处

  /// Handle a completed read of message data.
  template <typename T, typename Handler>
  void handle_read_data(const boost::system::error_code& e, std::size_t bytes_transferred,
      T& t, boost::tuple<Handler> handler)
  
    if (e)
    
      boost::get<0>(handler)(e);
    
    else
    
      // Extract the data structure from the data just received.
      try
      
        inbound_.commit(bytes_transferred);
        std::istream archive_stream(&inbound_);
        boost::archive::binary_iarchive archive(archive_stream);
        archive >> t;
      
      catch (std::exception& err)
      
        // Unable to decode data.
        boost::system::error_code error(boost::asio::error::invalid_argument);
        boost::get<0>(handler)(error);
        return;
      

      // Inform caller that data has been received ok.
      boost::get<0>(handler)(e);
    
  

【讨论】:

【参考方案2】:

在您的服务器端代码中,当您创建二进制存档时,streambuf 为空。如果存档构造函数在存档的开头寻找一个幻数,那么它不会找到它。尝试在构造流和存档之前调用boost::asio::read() 填充streambuf

【讨论】:

感谢您的回答。我在streambuf 定义之后立即移动了read()。现在 read 会抛出异常并显示此消息:End of file 我相信这是意料之中的。 read() 阻塞,直到缓冲区已满或发生错误。在这种情况下,“错误”是套接字已关闭。您可能希望使用 read() overload 通过 boost::system::error_code 引用参数返回状态,而不是抛出错误。

protobuf协议

...时会将其编译为二进制数据流,而当需要使用时再进行反序列化。protobuf支持几乎绝大多数的语言。通过proto文件,我们可以了解到一个数据的详细类型。在官方示例中可以看到一个数据类型的声明方法:message对象。在中则是添... 查看详情

3 位卡代码失败并出现错误 E00003 根据其数据类型,值 XXXXXXX 无效 - 模式约束失败

】3位卡代码失败并出现错误E00003根据其数据类型,值XXXXXXX无效-模式约束失败【英文标题】:3digitcardcodesfailingwitherrorE00003ThevalueXXXXXXXisinvalidaccordingtoitsdatatype-ThePatternconstraintfailed【发布时间】:2020-08-2514:45:37【问题描述】:代码... 查看详情

序列化 std::vector 以提升二进制存档时的数据相关失败

】序列化std::vector以提升二进制存档时的数据相关失败【英文标题】:Data-DependentFailureWhenSerializingstd::vectortoBoostBinaryArchive【发布时间】:2014-06-1917:59:37【问题描述】:刚开始熟悉Boost序列化库。我对似乎与数据相关的故障感到困... 查看详情

c#从数据库中读取图片出现参数无效

...quot;]);//创建内存流。MemoryStreammyStream=newMemoryStream();//图片二进制,写入内存foreach(byteainmybyte)myStream.WriteByte(a);//以内存数据流,创建图片对象。ImagemyImage=Image.FromStream(myStream);myStream.Close();//图片框赋值,显示出该图片对象。i++;在Im... 查看详情

boost 序列化:查看流是不是“好”

】boost序列化:查看流是不是“好”【英文标题】:boostserialization:seewhetherastreamis"good"boost序列化:查看流是否“好”【发布时间】:2011-10-3022:17:55【问题描述】:我目前正在开发一个服务器应用程序,它必须从客户端接... 查看详情

c#从数据库中读取图片出现参数无效

...quot;]);//创建内存流。MemoryStreammyStream=newMemoryStream();//图片二进制,写入内存foreach(byteainmybyte)myStream.WriteByte(a);//以内存数据流,创建图片对象。ImagemyImage=Image.FromStream(myStream);myStream.Close();//图片框赋值,显示出该图片对象。i++;在Im... 查看详情

构造“WebSocket”失败:URL“[object Object]”无效

...描述】:我试图与websocket建立连接并在反应路由器中获取数据。连接没有发生并且在浏览器控制台中出现错误UncaughtSyntaxError:Failedtoc 查看详情

使用 boost::serialization 将对象映射序列化为 xml

】使用boost::serialization将对象映射序列化为xml【英文标题】:Serializingamapofobjectstoxmlusingboost::serialization【发布时间】:2010-07-1907:07:36【问题描述】:下面的序列化示例来自boostmailinglist,这与我想做的几乎相同。但是,我更改了存... 查看详情

使用 Schema 验证无效的 json 数据不会因反序列化而失败

】使用Schema验证无效的json数据不会因反序列化而失败【英文标题】:ValidationofaninvalidjsondatawithSchemanotfailingwithDeserialization【发布时间】:2020-03-1107:19:39【问题描述】:我正在解析以下JSON:"names":"organizationNames":["name":"apple"]进入C#... 查看详情

部署到 AWS Elastic Beanstalk 失败,出现异常“UTF-8 中的字节序列无效”

】部署到AWSElasticBeanstalk失败,出现异常“UTF-8中的字节序列无效”【英文标题】:DeploymenttoAWSElasticBeanstalkfailingwithexception"invalidbytesequenceinUTF-8"【发布时间】:2018-07-0916:38:26【问题描述】:我在64位AmazonLinux发行版上的Elasti... 查看详情

ClickHouse 失败并出现错误“DirectoryMonitor:额外信息的校验和不匹配:损坏的数据”

...ata"【发布时间】:2020-10-1511:46:02【问题描述】:我的二进制文件在数据中带有曲线(我的错)。Cli 查看详情

使用 0.5 libyaml-cpp-dev 构建失败并出现 boost 错误

】使用0.5libyaml-cpp-dev构建失败并出现boost错误【英文标题】:Buildusing0.5libyaml-cpp-devfailswithboosterrors【发布时间】:2021-10-0508:13:42【问题描述】:我通过sudoaptinstalllibyaml-cpp-dev安装了libyaml-cpp-dev,并在我的机器上安装了yaml-cpp0.5版本... 查看详情

boost::geometry 中无效几何的数据集

】boost::geometry中无效几何的数据集【英文标题】:Datasetofinvalidgeometriesinboost::geometry【发布时间】:2018-04-1814:17:44【问题描述】:是否存在使用c++和boost::geometry库的所有可能无效几何的数据集?或者至少我可以转换为boost::geometry... 查看详情

Boost - 使用 TLV 编码序列化字符串

】Boost-使用TLV编码序列化字符串【英文标题】:Boost-serializestringwithTLVencoding【发布时间】:2014-06-1312:06:51【问题描述】:是否可以使用boost序列化具有TLV编码的字符串?或者我应该先编码,然后使用boost序列化二进制数据?如果... 查看详情

从 MVC 4 Web Api 返回匿名类型失败并出现序列化错误

】从MVC4WebApi返回匿名类型失败并出现序列化错误【英文标题】:ReturningananonymoustypefromMVC4WebApifailswithaserializationerror【发布时间】:2013-02-0409:48:11【问题描述】:我刚刚开始使用MVC4WebAPI,我似乎误解了它的工作原理。在使用WebAPI... 查看详情

如何显示流数据帧(显示失败并出现 AnalysisException)?

】如何显示流数据帧(显示失败并出现AnalysisException)?【英文标题】:HowtodisplayastreamingDataFrame(asshowfailswithAnalysisException)?【发布时间】:2017-12-1822:24:37【问题描述】:所以我有一些数据流在Kafka主题中,我正在获取这些流数据... 查看详情

您能否在应用程序提交状态“无效二进制”中出现错误“无效的 Swift 支持”和“缺少所需的图标文件”提供帮助?

】您能否在应用程序提交状态“无效二进制”中出现错误“无效的Swift支持”和“缺少所需的图标文件”提供帮助?【英文标题】:CanyouhelpinAppsubmissionstatus"InvalidBinary"witherror"InvalidSwiftSupport"and"Missingrequirediconfil... 查看详情

如何从 Db2 中的十进制字段中获取无效值列表?

...执行select*from时。它出现的错误是SQL0802类型6-无效的数字数据。我能够找到其中一些并修 查看详情