CZMQ:无法接收消息 [英] CZMQ : Unable to receive message

查看:30
本文介绍了CZMQ:无法接收消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的程序来演示使用libczmq"的发布者/订阅者通信.尽管我能够通过zmsg_send"api(或者我根据其返回值推测)发送消息,但我无法通过zmsg_recv"(阻塞)API 接收消息,它可能无法接收留言.

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.

推荐答案

默认情况下,SUB 套接字上没有订阅.尝试使用需要订阅的变体:

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.

这篇关于CZMQ:无法接收消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆