我的C ZeroMQ订阅者为什么没有收到任何数据?

我的(Python)发布者:

import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.PUB)
connectStr = "tcp://*:%d" % 5563
socket.bind(connectStr)

messageNum = 0
while True:
    ++messageNum
    message = "Testing %d"%messageNum
    print("Sending.. '%s'"%message)
    socket.send_string(message)
    time.sleep(1)
    messageNum += 1

我的(C)订阅者(在GTest中运行):

TEST(ZeroMqPubSubTest, SubscribeGetsData)
{

    // Set up the subscriber we'll use to receive the message.
    zmq::context_t context;
    zmq::socket_t subscriber(context, ZMQ_SUB);
    // Connect to the publisher
    subscriber.connect("tcp://127.0.0.1:5563");
    subscriber.setsockopt(ZMQ_SUBSCRIBE, ""); // Set the filter blank so we receive everything

    zmq::message_t response(0);
    EXPECT_TRUE(subscriber.recv(&response));
}

我启动发布者,然后启动订阅者.后者永远不会回来.

如果我运行一个Python订阅服务器,做(我认为)完全一样的事情.

import zmq

context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect ("tcp://127.0.0.1:5563")
socket.setsockopt_string(zmq.SUBSCRIBE, "")

print ("Waiting for data...")
while True:
    message = socket.recv()
    print ("Got some data:",message)

..它工作正常:

Waiting for data…

Got some data: b’Testing 8′

Got some data: b’Testing 9′

解决方法:

zmq.hpp中定义了setockopt的两个重载:

 template<typename T> void setsockopt(int option_, T const& optval)
 {
     setsockopt(option_, &optval, sizeof(T) );
 }

 inline void setsockopt (int option_, const void *optval_, size_t optvallen_)
 {
     int rc = zmq_setsockopt (ptr, option_, optval_, optvallen_);
     if (rc != 0)
         throw error_t ();
 }

通过仅提供两个参数,您隐式地使用了第一个重载,它假定了长度为sizeof(T)的值.解析为1,因为“”是一个以零结尾的字符数组.要传递一个空字符串,您需要使用第二个重载并将长度指定为0:

subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);

或者,使用零大小的数据类型:

char none[0];
subscriber.setsockopt(ZMQ_SUBSCRIBE, none);
上一篇:Unity_Dungeonize 随机生成迷宫


下一篇:POJ - 2251 Dungeon Master(搜索)