CZMQ:无法接收消息
CZMQ : Unable to receive message
我正在尝试编写一个简单的程序来演示使用 'libczmq' 的 Publisher/Subscriber 通信。虽然我可以通过 'zmsg_send' api 发送消息(或者我根据它的 return 值推测),但我无法通过 'zmsg_recv' 接收消息(blocking)API,可能收不到消息
#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;
}
下面是我构建和执行应用程序的方式。
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...
订阅者从未阅读 'message',请让我知道我在这里遗漏了什么。
默认情况下没有订阅 SUB 套接字。尝试使用需要订阅的变体:
zsock_t* zsock_new_sub (const char *endpoints, const char *subscribe)
或在 :
之后创建订阅
zsock_set_subscribe (void *zsock, const char *subscribe);
空字符串订阅一切
上面的程序有两个问题
- @david 在之前的评论中指出 'zsock_set_subscribe' 缺失。
- 发布者在订阅者有机会连接之前发送消息,因此消息将丢失,订阅者将继续等待永远不会到来的消息。
解决方案:
- 在订阅者套接字后添加'zsock_set_subscribe'
已初始化。
- 在发布者和之前初始化订阅者
在发布者之后立即添加一个小延迟(比如 1 秒)
以便为订户提供足够的时间进行连接和
最终收到消息。
我正在尝试编写一个简单的程序来演示使用 'libczmq' 的 Publisher/Subscriber 通信。虽然我可以通过 'zmsg_send' api 发送消息(或者我根据它的 return 值推测),但我无法通过 'zmsg_recv' 接收消息(blocking)API,可能收不到消息
#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;
}
下面是我构建和执行应用程序的方式。
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...
订阅者从未阅读 'message',请让我知道我在这里遗漏了什么。
默认情况下没有订阅 SUB 套接字。尝试使用需要订阅的变体:
zsock_t* zsock_new_sub (const char *endpoints, const char *subscribe)
或在 :
之后创建订阅zsock_set_subscribe (void *zsock, const char *subscribe);
空字符串订阅一切
上面的程序有两个问题
- @david 在之前的评论中指出 'zsock_set_subscribe' 缺失。
- 发布者在订阅者有机会连接之前发送消息,因此消息将丢失,订阅者将继续等待永远不会到来的消息。
解决方案:
- 在订阅者套接字后添加'zsock_set_subscribe' 已初始化。
- 在发布者和之前初始化订阅者 在发布者之后立即添加一个小延迟(比如 1 秒) 以便为订户提供足够的时间进行连接和 最终收到消息。