Refer to http://hintjens.wdfiles.com/local--files/main:files/cc1pe.pdf
Page 22 Chapter Divide and Conquer
Ventilator[PUSH]
___________________|____________________
| | |
[PULL]Worker[PUSH] [PULL]Worker[PUSH] [PULL]Worker[PUSH]
|__________________|___________________|
|
[PULL]Sink
// taskvent.c
// Socket to send messages on
void *context = zmq_ctx_new ();
void *sender = zmq_socket (context, ZMQ_PUSH);
zmq_bind (sender, "tcp://*:5557");
// Socket to send start of batch message on
void *sink = zmq_socket (context, ZMQ_PUSH);
zmq_connect (sink, "tcp://localhost:5558");
// taskwork.c
// Socket to receive messages on
void *context = zmq_ctx_new ();
void *receiver = zmq_socket (context, ZMQ_PULL);
zmq_connect (receiver, "tcp://localhost:5557");
// Socket to send messages to
void *sender = zmq_socket (context, ZMQ_PUSH);
zmq_connect (sender, "tcp://localhost:5558");
// tasksink.c
// Prepare our context and socket
void *context = zmq_ctx_new ();
void *receiver = zmq_socket (context, ZMQ_PULL);
zmq_bind (receiver, "tcp://*:5558");
I feel confused when to use zmq_bind or zmq_connect.
It says that most of time "Server" uses zmq_bind and "Client" uses zmq_connect.
Question> When I should use zmq_bind and when I should I use zmq_connect?
http://api.zeromq.org/
zmq_bind - accept incoming connections on a socket
zmq_connect - create outgoing connection from socket
答案 0 :(得分:17)
有一些基本原则可以绑定和连接。一般来说,zeromq并不关心*,这取决于你更方便。
对于一对彼此交谈的给定套接字,这里有几个问题需要弄清楚哪些应该绑定哪些应该连接:
这些主要是为了更轻松地管理网址和连接。在通风口/水槽示例中,只有一个呼吸机和一个水槽,但可以有任意数量的工人(零到多)。如果水槽和通风口都结合在一起,那么他们就不需要了解工人的来来往往。只有两个URL可以跟踪,而如果工作者绑定,则必须跟踪每个新工作人员的URL,并在每次新工作人员到达时告诉接收器和/或发布新URL。 / p>
*它在某些边缘情况下实际上很重要,但一般情况下并不重要。