mony
mony

Reputation: 3

Zero MQ publisher subscriber (pub/sub)

I have implemented a simple pub/sub example. I am sending a simple hello message and trying to receive it at the subscriber. My publisher code is -

std::string msg = "hello,";
zmq::message_t message(static_cast<const void*> (msg.data()), msg.size());
publisher.send(message);

My subscriber code -

zmq::message_t msgReceive;
subscriber.recv(&msgReceive);   
const char* buffer_body = static_cast<const char*>(msgReceive.data());
printf("Message: %s\n",buffer_body);

The output i am getting is - "hello, Socket-Type" instead of "hello,"

I am unable to figure out where is the mistake. Any help would be appreciated.

Upvotes: 0

Views: 1037

Answers (1)

ComradeJoecool
ComradeJoecool

Reputation: 824

Try changing this line

const char* buffer_body = static_cast<const char*>(msgReceive.data());

to

const char* buffer_body = static_cast<const char*>(msgReceive.data(), msgReceive.size());

That way you can tell the C_String where to terminate based on the length of the message rather than letting it terminate on its own when it finds the first null character.

For more information see the guide.

Upvotes: 1

Related Questions