natli
natli

Reputation: 3822

Application crashes when sending boost serialization over socket

I'm trying to use this C++ class as a base for my own application's client/server communication. Client and server both have a 'person' class, which I want to serialize:

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_;
};

I am trying to serialize and object of it on the server, send that serialization to the client, and create a new object out of it there.

server

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;
}

client

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::text_iarchive ia(ss); causes the crash; boost::archive::archive_exception at memory location

I had to comment it out, the crash is not suprising. Just look at what the server sends back.

client

As you can see, each time I type getObject, the server sends:

22
serialization::archive
9
0
0
31

And then it starts over. So I guess the application crashes because it's not receiving the complete serialized object. I also have no idea what most of those numbers are doing there and why they are being sent one by one.

What am I doing wrong?

Upvotes: 0

Views: 918

Answers (1)

Filip Ros&#233;en
Filip Ros&#233;en

Reputation: 63807

As you've already pointed out you are not sending the whole serialized data buffer.

    std::string mystring;
    ss >> mystring; //Serialization to string so we can send it

should be turned into

    std::string mystring (ss.str ());

Instead of reading up to the first whitespace we are now storing the entire serialized content in mystring.

Upvotes: 1

Related Questions