Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Pubsub envelope publisher
- // Note that the zhelpers.h file also provides s_sendmore
- #include "zhelpers.h"
- int main (void)
- {
- char *endpoint = "tcp://localhost:5678";
- // char *endpoint = "ipc:///tmp/wat/5563";
- // Prepare our context and publisher
- pid_t child_pid = fork();
- if (child_pid >= 0) {
- if (child_pid == 0) {
- sleep(5);
- // Prepare our context and subscriber
- void *context = zmq_ctx_new ();
- void *subscriber = zmq_socket (context, ZMQ_SUB);
- zmq_connect (subscriber, endpoint);
- zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "B", 1);
- while (1) {
- // Read envelope with address
- char *address = s_recv (subscriber);
- // Read message contents
- char *contents = s_recv (subscriber);
- printf ("[%s] %s\n", address, contents);
- free (address);
- free (contents);
- }
- }
- else {
- void *context = zmq_ctx_new ();
- void *publisher = zmq_socket (context, ZMQ_PUB);
- zmq_bind (publisher, endpoint);
- while (1) {
- // Write two messages, each with an envelope and content
- s_sendmore (publisher, "A");
- s_send (publisher, "We don't want to see this");
- s_sendmore (publisher, "B");
- s_send (publisher, "We would like to see this");
- printf ("published 2 messages\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment