this C++クラスを自分のアプリケーションのクライアント/サーバー通信のベースとして使用しようとしています。私は、シリアル化し、サーバー上のそれのオブジェクト、クライアントにそのシリアライズを送信し、新しいオブジェクトを作成しようとしていますソケット上でブーストシリアル化を送信するとアプリケーションがクラッシュする
class person
{
public:
person()
{
}
person(int age)
: age_(age)
{
}
int age() const
{
return age_;
}
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & age_;
}
int age_;
};
:クライアントとサーバーは、私がシリアライズしたい "人のクラスを、持っている両方のそこにそれの。
サーバー
while(1)
{
string clientMessageIn = "";
// receive from the client
int numBytes = client->recieveMessage(clientMessageIn);
if (numBytes == -99) break;
if(clientMessageIn == "getObject") //Client asked for object
{
boost::archive::text_oarchive oa(ss);
person pi(31); //Create 31 year old person
oa << pi; //Serialize
std::string mystring;
ss >> mystring; //Serialization to string so we can send it
string sendMsg(mystring); //Set sendMsg (redundant.. probably)
mystring.clear(); //No longer need mystring
client->sendMessage(sendMsg); //Send the actual response to the client
sendMsg.clear(); //Clear
ss.clear(); //Clear
}
else //Client typed something else, just show it
cout << "[RECV:" << clientHost << "]: " << clientMessageIn << endl;
}
クライアント
クラッシュを引き起こしboost::archive::text_iarchive ia(ss);
int recvBytes = 0;
while (1)
{
// send message to server
char sendmsg[MAX_MSG_LEN+1];
memset(sendmsg,0,sizeof(sendmsg));
cout << "[" << localHostName << ":SEND] ";
cin.getline(sendmsg,MAX_MSG_LEN);
string sendMsg(sendmsg);
if (sendMsg.compare("Bye") == 0 || sendMsg.compare("bye") == 0) break;
myClient.sendMessage(sendMsg);
// receive response from server
string clientMessageIn = "";
recvBytes = myClient.recieveMessage(clientMessageIn);
if (recvBytes == -99) break;
//stringstream ss;
//ss << clientMessageIn; //Server response to ss
//boost::archive::text_iarchive ia(ss); //This bit is causing the crash
//person p;
//ia >> p; //Unserialize
//ss.clear(); //No longer need the ss contents
//cout << "[RECV:" << serverName << "]: " << p.age<< endl; //This doesn't work now
cout << "[RECV:" << serverName << "]: " << clientMessageIn << endl;
}
。 boost::archive::archive_exception at memory location
私はそれをコメントしなければならなかった、クラッシュは驚くべきことではありません。サーバーが返すものを見てください。
22
serialization::archive
9
0
0
31
をし、それは最初からやり直し:
あなたが見ることができるように、私はgetObjectメソッドを入力するたびに、サーバーが送信します。だから私は完全なシリアル化されたオブジェクトを受信していないので、アプリケーションがクラッシュすると思います。私はまた、それらの数字のほとんどがそこで何をしているのか、そしてなぜ彼らが1つずつ送られているのか分かりません。
私は間違っていますか?
おかげで、魅力的なように働いていました。私はこれらの些細なことをつまずくと信じられません; O – natli