You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Boost UDP客户端反序列化Vector抛出archive_exception问题求助

Boost序列化UDP传输多记录时反序列化报错问题

我之前看到过一篇讨论boost::archive::archive_exception的帖子,但它针对的是文件序列化场景,和我现在用UDP传输Vector序列化数据的需求完全不匹配,没能解决我的问题。

具体情况是这样的:我在服务端完成Vector的序列化,通过UDP发送到客户端后进行反序列化。当Vector里只有1-2条记录时,整个流程一切正常,但一旦记录数超过2条,客户端在反序列化环节就会抛出错误:Microsoft C++ exception: boost::archive::archive_exception at memory location。

服务端序列化代码

//I think these headers are enough !!! 
#include <boost/archive/text_iarchive.hpp> 
#include <boost/archive/text_oarchive.hpp> 
#include<boost/serialization/vector.hpp> 
#include<boost/serialization/map.hpp> 

template<class input> 
void UdpServer::serializeData(input &vector) { 
    // Here i use vector in parameter 
    std::ostringstream archive_stream; 
    boost::archive::text_oarchive archive(archive_stream); 
    archive << vector; 
    sendMessageToClients(archive_stream.str());//sending to client 
} 

客户端报错的反序列化代码

#include <boost/archive/text_iarchive.hpp> 
#include <boost/archive/text_oarchive.hpp> 
#include<boost/serialization/vector.hpp> 
#include<boost/serialization/map.hpp> 

//this is what i received from the server 
size_t const len = m_socket.receive_from( 
    boost::asio::buffer(recv_buf), 
    m_sender_endpoint); 
std::string const received_message(recv_buf.data(), len); 
std::string archive_data(received_message);// receive from server 
std::istringstream archive_stream(archive_data); 
boost::archive::text_iarchive archive(archive_stream); 
archive >> map; 
displayCarData(map); //i'm passing this to function for printing the values 

后来我尝试引入Boost io binary库修改了客户端代码,结果错误变成了c++ out of range memory location,修改后的代码如下:

std::string archive_data(received_message); 
std::istringstream archive_stream(archive_data); 
boost::archive::text_iarchive archive(archive_stream, boost::io::binary); 
archive >> map; 
displayCarData(map); 

实在找不到解决办法了,恳请各位大佬帮忙看看,谢谢!

内容的提问来源于stack exchange,提问作者PANKAJ NAROLA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:03:19