4
我想在ZMQ 4.2.3和cppzmq中使用子/ pub模式接收多部分消息我能夠成功發送和接收單個部分消息,但是當我嘗試在第二幀中讀取它的大小始終爲0.我完全困惑的是使用NetMQ的C#版本讀取第二幀沒有問題,導致我相信它正在正確發送。我知道我錯過了一些東西,但今天是我試圖找出沒有成功的第二天。ZeroMQ多部分消息第二部分0尺寸
這裏是我的酒吧代碼
#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);
}
}
而我的C++子代碼
#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";
}
}
隨着
Recv returned true! ragefire.bob
There's more! Size is 0
輸出爲了完整起見,我NetMQ子,可以讀取兩個框架
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)}");
}
}
哇,我無法相信我錯過了,這是快把我逼瘋了,謝謝! –