我的(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)
..工作正常:
等待数据......
得到一些数据:b'Testing 8'
得到一些数据:b'Testing 9'
答案 0 :(得分:2)
zmq.hpp
中定义了setsockopt
的两个重载:
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)
。这解析为一,因为""
是一个以零结尾的字符数组。要传入一个空字符串,您需要使用第二个重载并指定长度0:
subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);
或者,使用零大小数据类型:
char none[0];
subscriber.setsockopt(ZMQ_SUBSCRIBE, none);