ZeroMQ: How to send messages to a specific subscriber
In my scenario there are n clients and 1 server. The clients send requests to the server and receive responses. Also, a one-way-messaging mechanism is required, where the server sends specific messages to specific clients and expects no answer. So the server must be able to address clients specifically with the one-way-messaging mechanism.
The request-response part is easy (REQ and REP sockets), but I am not sure how to implement the one-way-messaging part. I tried using PUB and SUB sockets with each subscriber requesting an ID from the server and subscribing only to messages that have this ID like this:
rc = zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE,
subscriber_id, strlen (subscriber_id));
This works in theory, but the problem is that the messages are all broadcasted and then filtered on the client side, which is not an option for me.
So my question is: Is there a better way to address a specific client?
So, there's only two ways to do what you want, and neither of them hit the target directly - but at the end I make a suggestion that might work better anyway.
Your requirement to have the server socket address the client sockets directly can only be accomplished in the following two ways:
If it needs to be a one-way socket type, then the second method is the only option you're left with. You can then choose any one-way communication strategy (PUB/SUB, PUSH/PULL) and just set up a new socket per connection.
I would strongly recommend the ROUTER option, there's almost no reason to force the one-way requirement. Just set your receive HWM to 1 on your ROUTER and ignore or discard messages sent to it periodically. Or, skip the REP/REQ pair and just do everything with ROUTER/DEALER, which is probably the best choice.
链接地址: http://www.djcxy.com/p/18378.html上一篇: 如何在这种情况下使用ZeroMQ设计结构
下一篇: ZeroMQ:如何将消息发送给特定用户