CZMQ : Unable to receive message

不想你离开。 提交于 2019-12-07 22:53:23

问题


I am trying to write a simple program to demonstrate Publisher/Subscriber communication using 'libczmq'. Although I am able to send the message via the 'zmsg_send' api ( or so I presume based on its return value ), I am unable to receive the message via the 'zmsg_recv' (blocking) API, it is probably unable to receive the message.

#include "czmq.h"
int main (void)
{
  int rc;
  const char *ipc_file="ipc://tmp.socket";
  const char *str = "Hello World!!!";

  /*****************************************/
  /* Creating and binding publisher Socket */
  /*****************************************/
  zsock_t *pub_sock = zsock_new(ZMQ_PUB);
  assert(pub_sock!=NULL);
  rc = zsock_bind(pub_sock, ipc_file, NULL);
  assert(rc==0);

  /**************************************************/
  /* Creating and connecting with Subscriber Socket */
  /**************************************************/
  zsock_t *sub_sock = zsock_new(ZMQ_SUB);
  assert(sub_sock);
  rc = zsock_connect(sub_sock, ipc_file, NULL);
  assert(rc==0);

  /***************************************/
  /* Creating messager & Frame instances */
  /*         and sending message         */
  /***************************************/
  zmsg_t *msg = zmsg_new ();
  assert(msg!=NULL);
  zframe_t *frame = zframe_new (str, strlen(str));
  assert(frame!=NULL);
  zmsg_prepend(msg, &frame);
  printf("PUB : frame_count = %u, content_size = %d, msg_ptr = %p\n",
        zmsg_size (msg), zmsg_content_size(msg), msg);
  rc = zmsg_send(&msg, pub_sock);
  assert (rc == 0);
  printf("PUB : Message send successfully...\n");

  /********************************/
  /* Subscriber receiving message */
  /********************************/
  printf("SUB : Reading message...\n");
  msg = zmsg_recv(sub_sock);
  assert(msg!=NULL);
  printf("SUB : frame_count = %u, content_size = %d, msg_ptr = %p\n",
        zmsg_size (msg), zmsg_content_size(msg), msg);
  frame = zmsg_pop(msg);
  assert(frame!=NULL);
  printf("SUB : received in frame = \"%s\"\n", zframe_data (frame));

  zmsg_destroy (&msg);
  zframe_destroy (&frame);
  zsock_destroy (&sub_sock);
  zsock_destroy (&pub_sock);
  return 0;
}

Below is how I built and executed the application.

user@debian:~/progs/czmq$ make
cc -Iczmq/include -ggdb   -c -o pub-sub-test.o pub-sub-test.c
gcc -L./czmq/src/.libs -lzmq -lczmq -lpthread pub-sub-test.o -o ../pub-sub-test
user@debian:~/progs/czmq$ ../pub-sub-test
PUB : frame_count = 1, content_size = 14, msg_ptr = 0x1653b10
PUB : Message send successfully...
SUB : Reading message...

The 'message' is never read by the subscriber, please let me know what I am missing here.


回答1:


By default there is no subscription on a SUB socket. Try using the variant that takes a subscription :

zsock_t* zsock_new_sub (const char *endpoints, const char *subscribe)

Or create the subscription after :

zsock_set_subscribe (void *zsock, const char *subscribe);

The empty string subscribes to everything.




回答2:


There two problems in the above program.

  1. The absence of 'zsock_set_subscribe' as @david points out in an earlier comment.
  2. The publisher sends the message before subscriber has an opportunity to connect, hence the message would be lost and the subscriber would continue to wait for a message which would never come.

Solution :

  1. Adding the 'zsock_set_subscribe' after the subscriber socket is initialized.
  2. Initializing the subscriber before the publisher and adding a small delay (say 1 sec) immediately after the publisher' so as to provide enough time for the subscriber to connect and eventually receive the message.


来源:https://stackoverflow.com/questions/39290802/czmq-unable-to-receive-message

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!