添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

First subscriptions seem to be dropped when a PUB connects to a SUB #2267

Open
@sublee

Description

I usually use PUB sockets as clients of SUB sockets. On this topology, first subscriptions from SUB sockets always seem to be dropped.

@kexplo who is my co-worker made a code to reproduce this issue by C++. There is a switch constant named PUB_CONNECTS . If it is true , a PUB socket connects to a SUB socket then the SUB socket won't receive the message the PUB socket sent. Otherwise, a SUB socket connects to a PUB socket and everything is going to be fine:

#include <zmq.h>
#include <iostream>
#include <unistd.h>
// When a SUB socket binds to an address then a PUB socket connects to the
// SUB socket, the first subscription seems to be dropped.
const bool PUB_CONNECTS = true;
int main(int argc, char *argv[])
  void *context = zmq_ctx_new();
  void *pub = zmq_socket(context, ZMQ_PUB);
  void *sub = zmq_socket(context, ZMQ_SUB);
  // Make one of PUB or SUB sockets be a server.
  void *server = 0;
  void *client = 0;
  if (PUB_CONNECTS)
    server = sub;
    client = pub;
    server = pub;
    client = sub;
  zmq_bind(server, "tcp://127.0.0.1:5932");
  zmq_connect(client, "tcp://127.0.0.1:5932");
  // Start to subscribe "hello".
  zmq_setsockopt(sub, ZMQ_SUBSCRIBE, "hello", 5);
  usleep(1000);
  // Publish and receive a message.
  zmq_send(pub, "hello world", 11, 0);
  std::cout << "SUB is waiting for an incoming message..." << std::endl;
  char recv_buf[40] = { 0 };
  int n_bytes = zmq_recv(sub, recv_buf, 40, 0);
  if (n_bytes > 0)
    std::cout << recv_buf << std::endl;
  // Finalize.
  zmq_close(sub);
  zmq_close(pub);
  zmq_ctx_destroy(context);
  return 0;

I tested with ZeroMQ-4.2.0.