boost serialiaze archive转换为char

时间:2015-05-11 21:06:39

标签: c++ serialization boost

我已使用以下代码成功序列化了一个对象:

void aTodo::SendBack(protocolBaseServer & in){
   std::ostringstream archive_stream;
   boost::archive::text_oarchive archive(archive_stream);
   archive << *this;
   std::string outbound_data=archive_stream.str();
//type needs to be first for factory to create correct object
   outbound_data=_initType+outbound_data;
   //now just send it along us
   in.DoWrite(outbound_data);
}

这是protocolBaseSer DoWrite:

void protocolBaseServer::DoWrite(std::string inMessage){
   _totalMessage=inMessage;
   std::cout << "totalMessage: " << _totalMessage << std::endl;
   int32_t dataLength=_totalMessage.length();
   int32_t orgdataLength=_totalMessage.length();

   std::cout << "data length: " << orgdataLength << std::endl;
   //read in todo
   dataLength = htonl(dataLength ); // Ensure host system byte order on the int;from the network will be network byte order
   std::cout << "writing length now" << std::endl;
   protoService<int32_t>::DoWrite(_sock,&dataLength,sizeof(int32_t));
   std::cout << "written length" << std::endl;
   std::cout << "writing :" << _totalMessage.data() << std::endl;
   protoService<int32_t>::DoWrite(_sock,(void*)_totalMessage.data(),orgdataLength); // send the string dataEE
   std::cout << "wrote :" << _totalMessage << std::endl;

}

为什么当我将_totalMessage转换为c字符串以通过套接字发送它时,它什么也没发送。

当我尝试将_totalMessage.c_str()打印到屏幕时,它什么都没打印?这是上面的输出。你可以看到&#34;写作:&#34;线没有显示的东西。我觉得有什么东西我将这个存档转换为字符数组以通过我的套接字发送?

writing :
Size to write: 38
wrote :22 serialization::archive 12 0 0 0 0

有趣的是,准备发送字符串的函数确实输出&#34; outbound.c:&#34;下面:

void aTodo::SendBack(protocolBaseServer & in){
   std::ostringstream archive_stream;
   {  
   boost::archive::text_oarchive archive(archive_stream);
   archive << *this;
   }  
   archive_stream.flush();
   std::string outbound_data=archive_stream.str();
//type needs to be first for factory to create correct object
   std::cout << "outbound: " << outbound_data << std::endl;
   std::cout << "outbound.c: " << outbound_data.c_str() << std::endl;
   outbound_data=_initType+outbound_data;
   //now just send it along us
   in.DoWrite(outbound_data);
}

编辑。 我似乎找到了罪魁祸首。 我想在出站字符串前加一个字符,所以我这样做: _initType是一个const char = 0x01。

看起来你不能使用+运算符将const char连接到字符串。它会搞砸.c_str()返回。

void aTodo::SendBack(protocolBaseServer & in){
   std::ostringstream archive_stream;
   {  
   boost::archive::text_oarchive archive(archive_stream);
   archive << *this;
   }  
   archive_stream.flush();
   std::string outbound_data=archive_stream.str();
//type needs to be first for factory to create correct object
   std::cout << "outbound: " << outbound_data << std::endl;
   std::cout << "outbound.c: " << outbound_data.c_str() << std::endl;
   outbound_data=_initType+outbound_data;
   //now just send it along us
   in.DoWrite(outbound_data);
}

我明白了。 我认为我的_initType变量没有被设置为0x01。它被设置为0x00。

1 个答案:

答案 0 :(得分:0)

在使用结果之前,您可能需要关闭存档:

{
    boost::archive::text_oarchive archive(archive_stream);
    archive << *this;
}
archive_stream.flush();
std::string outbound_data=archive_stream.str();