我正在尝试使用boost序列化在套接字(非boost)上发送类Student的对象。我能够序列化数据并通过套接字发送它。 现在问题是我在另一边收到它(好)。但是当我尝试反序列化它时,我得到了boost存档异常:输入流错误。 不知道我在哪里弄错了。 代码:
class Student
{
public:
double Age;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & Age;
}
};
发件人:
Student one;
one.Age = 16.50;
std::stringstream ofs;
// save data to archive
{
boost::archive::text_oarchive oa(ofs);
oa << one;
}
write(sockfd, ofs.str().c_str(), sizeof (struct Student));
接收结束:
Student stud;
char buf[512];
read( recv_sock , &buf, sizeof (struct Student));
std::string temp;
temp.assign(buf);
std::cout<<temp<<std::endl;
std::cout<<"Read bytes "<<valread<<" bytes<<"<<buf<<"\n";
std::stringstream ifs;
ifs<<temp;
{
boost::archive::text_iarchive ia(ifs); // Exception occurs here
ia >> stud;
}
对此问题的任何指示都将不胜感激