升压序列化的二进制数据失败'无效签名“错误 [英] Serializing binary data in boost fails with `invalid signature' error

查看:125
本文介绍了升压序列化的二进制数据失败'无效签名“错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在遇到困难搞清楚使用boost序列化/ ASIO到通过网络发送对象的正确途径。在的消息类尽可能简单。这不是C ++友好的也不适合我的需要,我只是保持简单暂时ASIO测试/ SER:

 类消息{
    友元类的boost ::系列化::访问;
上市:
    信息(){}
    int型的;
    INT发送;
    INT分配;
    INT nogood;    模板<类归档和GT;
    无效连载(归档和放大器; AR,const的无符号整型版)
    {
        AR&安培;类型;
        AR&安培;发送者;
        AR&安培;分配;
        AR&安培;不好;
    }
};

在当代理决定发送一条消息,将其发送到服务器通过其客户端的TCP连接:​​

 消息m;
//做一些事情来生成消息
提高:: ASIO ::流缓冲bufx;
的std :: ostream的OS(安培; bufx);
提高::档案:: binary_oarchive AR(OS);
AR&安培;米;
提高:: ASIO ::写(插座,bufx);

服务器端code:

 的boost ::支持ASIO ::流缓冲bufx;
的std :: istream处于(安培; bufx);
提高::档案:: binary_iarchive IA(是); //< ---异常:无效的签名
为size_t RCX = ASIO ::阅读(插座,bufx);
消息m;
IA>>米;


解决方案

在你的服务器端code,你的流缓冲是空的,当你创建二进制归档。如果存档构造正在寻找在归档文件的开头一个神奇的数字,那么它不会找到它。尝试与呼叫填充流缓冲的boost :: ASIO ::阅读() 之前构建流和存档。

I'm having difficulties figuring out correct way of using boost serialization/asio to send objects over network. The message class is as simple as possible. It's not C++ friendly nor suitable for my needs, I just keep it simple temporarily to test 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;
    }
};

On the client side when agent decides to send a message, sends it to server over it's tcp connection:

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);

Server side code:

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;

解决方案

In your server-side code, your streambuf is empty when you create the binary archive. If the archive constructor is looking for a magic number at the beginning of the archive then it won't find it. Try filling the streambuf with the call to boost::asio::read() before constructing the stream and archive.

这篇关于升压序列化的二进制数据失败'无效签名“错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆