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.
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.
There two problems in the above program.
- The absence of 'zsock_set_subscribe' as @david points out in an earlier comment.
- 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 :
- Adding the 'zsock_set_subscribe' after the subscriber socket is initialized.
- 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