PUB/SUB 我可以在 .bind() 之前使用 .connect() 吗?

PUB/SUB can I .connect() before I .bind()?

我正在使用 PUB/SUB 设计,我的问题是:

我可以 .bind() 在另一个套接字 .connect() 连接到端口后, 或者我应该 .bind() 在另一个套接字尝试 .connect() 到同一地址之前?

换句话说:

.bind().connect() 的顺序重要吗?

(我认为这个问题并不特定于 PUB/SUB,而是与任何设计相关)。

ZMQ 背后的一个驱动原则是不担心您尝试连接的套接字是否已经存在。这些是 ZMQ 试图从开发人员那里抽象出来的细节。所以,不,bind()connect() 的顺序对任何套接字类型都无关紧要。

我建议你阅读the zmq guide if you plan to do extensive work with it, relevant portion here:

Remember that ZeroMQ does asynchronous I/O, i.e., in the background. Say you have two nodes doing this, in this order:

  • Subscriber connects to an endpoint and receives and counts messages.
  • Publisher binds to an endpoint and immediately sends 1,000 messages.

Then the subscriber will most likely not receive anything. You'll blink, check that you set a correct filter and try again, and the subscriber will still not receive anything.

...这里有一点需要注意,因为 PUB/SUB - 即使您 connect() 首先与您的订阅者联系,该连接实际上 [=24] =] 在发布者 之后 发生 bind()-ed,因此如果您尝试与发布者发送消息而不等待订阅者完成连接,这些消息将永远不会发送给您的订阅者。

这取决于单播传输,tcp://ipc:// transport-classes 断开连接,因此 .bind().connect() 的顺序无关紧要。

但是连接了一个inproc://transport-class,所以需要先.bind().connect()

http://zguide.zeromq.org/page:all#Unicast-Transports