Reputation: 53
I'm trying to receive a multi part message using a sub/pub pattern in ZMQ 4.2.3 and cppzmq I'm able to successfully send and receive single part messages but when I try to read in the second frame it's size is always 0. What has me completely baffled is a C# version using NetMQ reads the second frame without issue, causing me to believe it is being sent properly. I know I'm missing something but today is my second day of trying to figure out what with no success.
Here is my pub code
#include <iostream>
#include "zmq_addon.hpp"
void main()
{
zmq::context_t ctx = zmq::context_t();
zmq::socket_t pub = zmq::socket_t(ctx, zmq::socket_type::pub);
try
{
//binding using localhost gives an error about invalid device?
pub.bind("tcp://*:8845");
}
catch (...)
{
std::cout << zmq_strerror(zmq_errno());
std::cin.get();
return;
}
byte topic = 8;
std::string id = "ragefire.bob";
while (true)
{
std::cout << "Spam\n";
pub.send(id.c_str(), id.length(), ZMQ_SNDMORE);
pub.send("foo", 3);
}
}
And my C++ sub code
#include <iostream>
#include "zmq_addon.hpp"
int main()
{
zmq::context_t ctx = zmq::context_t();
zmq::socket_t sub = zmq::socket_t(ctx, zmq::socket_type::sub);
sub.connect("tcp://localhost:8845");
std::string id = "ragefire.bob";
sub.setsockopt(ZMQ_SUBSCRIBE, id.c_str(), id.length());
while (true)
{
zmq::message_t msg;
if(sub.recv(&msg,ZMQ_NOBLOCK))
{
auto rpl = std::string(static_cast<char*>(msg.data()), msg.size());
std::cout << "Recv returned true! " << rpl << "\n";
int more;
auto more_size = sizeof(more);
sub.getsockopt(ZMQ_RCVMORE, &more, &more_size);
while (more)
{
zmq::message_t moreMsg;
sub.recv(&msg);
std::string moreRpl = std::string(static_cast<char*>(moreMsg.data()), moreMsg.size());
std::cout << "There's more! " << moreRpl << "Size is " << moreMsg.size() << "\n";
sub.getsockopt(ZMQ_RCVMORE, &more, &more_size);
}
}
else
std::cout << "Recv returned false! " << zmq_strerror(zmq_errno()) << "\n";
}
}
With an output of
Recv returned true! ragefire.bob
There's more! Size is 0
For completeness, my NetMQ sub that can read both of the frames
static void Main()
{
SubscriberSocket sub = new SubscriberSocket("tcp://localhost:8845");
sub.SubscribeToAnyTopic();
while (true)
{
NetMQMessage msg = sub.ReceiveMultipartMessage();
Console.WriteLine($"Received! {Encoding.ASCII.GetString(msg.First.Buffer)}");
Console.WriteLine($"Received! {Encoding.ASCII.GetString(msg[1].Buffer)}");
}
}
Upvotes: 5
Views: 2098
Reputation: 844
Your initial call to sub.recv()
is receiving the message in &msg
, which is fine.
Your subsequent call to sub.recv()
is also receiving into &msg
, but you are then reporting about moreMsg
. If you really want to keep a second message_t
around, do
sub.recv(&moreMsg);
Upvotes: 3