[zeromq-dev] How to add pub In "XPUB/XSUB" model?Thans a lot.
杨云召
yangyunzhao at qq.com
Wed Oct 18 10:34:00 CEST 2017
Hi All:
Sorry for my poor englith.
In my requirement, it have 0..N publishers and 0...N subscribers.
I have 3 programs such as follow.
I can freedom add/sub subscriber which only know the proxy ip info.
But I can not add or sub any publisher when proxy is running.
Could you give me some tips about how to add/sub a publisher in "XPUB/XSUB" model?
Thanks a lot.
proxy:
int main(int argc, char ** argv)
{
void * ctx = zmq_ctx_new();
void * xsub = zmq_socket(ctx, ZMQ_XSUB);
zmq_connect(xsub, "tcp://192.168.2.42:6000"); /// one publiser
zmq_connect(xsub, "tcp://192.168.2.6:6000"); /// another publisher
void * xpub = zmq_socket(ctx, ZMQ_XPUB);
zmq_bind(xpub, "tcp://192.168.2.6:6001");
zmq_proxy(xsub, xpub, NULL);
return 0;
}
pub:
int main(int argc, char ** argv)
{
void * ctx = zmq_ctx_new();
void * pub = zmq_socket(ctx, ZMQ_PUB);
zmq_bind(pub, "tcp://*:6000");
while (true)
{
std::stringstream ss;
ss << time(NULL) << "-" << argv[1];
zmq_send(pub, ss.str().c_str(), ss.str().size(), 0);
Sleep(1000);
}
}
sub:
int main(int argc, char ** argv)
{
void * ctx = zmq_ctx_new();
void * sub = zmq_socket(ctx, ZMQ_SUB);
zmq_connect(sub, "tcp://192.168.2.6:6001");
zmq_setsockopt(sub, ZMQ_SUBSCRIBE, 0, 0);
while (true)
{
char buffer[256];
int size = zmq_recv(sub, buffer, 255, 0);
if (size == -1)
{
Sleep(0);
continue;
}
buffer[size] = '\0';
printf("run_sub == %s\n", buffer);
}
}
yangyunzhao at qq.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20171018/51f54f61/attachment.htm>
More information about the zeromq-dev
mailing list