Trying to get to grips with how to serialize an object and send/receive it
via zeromq I've modified the Hello World example from the zeromq
documentation to try and achieve this. Code is below.
Whilst the server appears to be outputting a sensible:
Sent Reply: 22 serialization::archive 10 0 0 1 12 Test Message
When this is received at the client end it is a jumbled mess of random
characters.
Does anyone know what I'm not doing correctly here?
Thanks,
Riskybiz.
//Server
//Intended to send a serialized object instance to a client
#include <zmq.hpp>
#include <string>
#include <sstream>
#include <boost\archive\text_oarchive.hpp>
#include "TestClass.h"
int main () {
//Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
socket.bind ("tcp://*:4444");
//Create an object instance and serialize it
TestClass obj1(1, "Test Message");
std::ostringstream buffer;
boost::archive::text_oarchive archive(buffer);
archive & obj1;//serialize the object
while (true)
{
zmq::message_t request;
// Wait for next request from client
socket.recv(&request);
//Cast the request data to a string
std::string reqStr( static_cast<char*>(request.data()), request.size() );
std::cout << "Received: " << reqStr << std::endl;
// Do some 'work'
Sleep(1000);//1000 millisecond pause
std::string outStr(buffer.str());//Get the serialization data (a string?)
from the buffer
//Send data to client
zmq::message_t reply(outStr.size());
memcpy((void *) reply.data(), &outStr, outStr.size());
socket.send(reply);
std::cout << "Sent Reply: " << outStr << std::endl;
}
return 0;
}
//Client
//Intended to send a request to the server and receive, then deserialize an
object
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <sstream>
#include <boost\archive\text_iarchive.hpp>
#include "TestClass.h"
int main ()
{
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REQ);
std::cout << "Connecting to hello world server...." << std::endl;
socket.connect ("tcp://localhost:4444");
// Do 10 requests, waiting each time for a response
for (int request_nbr = 0; request_nbr != 10; request_nbr++) {
char req[] = "Hello";
zmq::message_t request (strlen(req)+1);
memcpy ((void *) request.data (), req, strlen(req)+1);
std::cout << "Sending Hello " << request_nbr << "...." << std::endl;
socket.send (request);
// Get the reply.
zmq::message_t reply;
socket.recv (&reply);
std::cout << "Received the reply...." << std::endl;
TestClass obj1;
std::cout << "Instantiated TestClass Object...." << std::endl;
std::string repStr( static_cast<char*>(reply.data()), reply.size() );//cast
received message data to a string
std::cout << "Received Message: " << repStr << std::endl;
std::istringstream buffer(repStr);//initialise with received serialized
string data
std::cout << "Initialised buffer...." << std::endl;
boost::archive::text_iarchive archive(buffer);
std::cout << "Prepared archive...." << std::endl;
archive & obj1;//deserialise the archive into TestClass instance
std::cout << "Deserialized...." << std::endl;
//Check that data members of the deserialised object may be accessed
std::cout << "Received: " << request_nbr << " ID: " << obj1.ID << " Message:
" << obj1.text << std::endl;
}
return 0;
}
//TestClass Definition
#ifndef TEST_CLASS_H
#define TEST_CLASS_H
#include <string>
#include <boost\archive\text_oarchive.hpp>
#include <boost\archive\text_iarchive.hpp>
class TestClass
{
public:
TestClass():ID(0), text("blank")//default constructor
{
}
TestClass(const unsigned int id, const std::string txt):ID(id),
text(txt)//constructor
{
}
TestClass(const TestClass&);//copy constructor
~TestClass()//destructor
{
}
const unsigned int ID;
const std::string text;
private:
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive& archive, const unsigned version)
{
archive & const_cast<unsigned int&>(ID) &
const_cast<std::string&>(text);
}
};//class
#endif
_______________________________________________
zeromq-dev mailing list
[email protected]
http://lists.zeromq.org/mailman/listinfo/zeromq-dev