简体   繁体   English

如何将序列化数据传输到服务器并反序列化

[英]How to transfer serialized data to the server and deserelize it

I use the enet library to write the client and server side of the code, and also decided to use the boost library to serialize the data.我用enet库写了客户端和服务器端的代码,也决定用boost库来序列化数据。 I used something like this code to send data and serialize it我使用类似这段代码的东西来发送数据并序列化它

        char message_data[80] = "somedata";
        std::stringstream ss;
        boost::archive::binary_oarchive oa{ ss };
        oa << message_data;
        SendPacket(peer, ss.str());

and something like this code for getting data and deserializing it以及类似这样的代码,用于获取数据并对其进行反序列化

                std::string deser_data;
                std::string some_data = (char*)event.packet->data;
                std::stringstream ss(some_data);
                boost::archive::binary_iarchive ia{ ss };
                ia >> deser_data;

But this code does not work, I think it is due to incorrect use of stringstream, but this is not accurate但是这段代码不起作用,我认为是由于错误使用了stringstream,但这并不准确

Is there some reason you want to use a binary (non-portable, change-intolerant) representation?您是否有某些原因要使用二进制(不可移植、不容忍更改)表示? JSON has become a bit of a standard. JSON 已经有点标准了。 Be that as it may...是因为它可能...

I would add some debug.我会添加一些调试。 What length are you writing?你写的是多长? What length are you reading?你读的是多长? If you dump the buffers, are they identical on client and server?如果您转储缓冲区,它们在客户端和服务器上是否相同?

Does the code work if you do it all inline (no network involved)?如果您全部内联(不涉及网络),代码是否有效? That is, can you write a unit test to do both the client and server side where you construct an object, serialize it, deserialize into another object, and then compare them?也就是说,你能不能写一个单元测试来做客户端和服务器端,你构造一个object,序列化它,反序列化成另一个object,然后比较它们?

I'd start with that.我会从那开始。

Sorry, this isn't a proper answer, but it's too long for a simple comment.抱歉,这不是一个正确的答案,但是对于简单的评论来说太长了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM