[zeromq-dev] Pub Sub with pgm example

Susan Tharakan susantharakan83 at gmail.com
Mon Mar 18 07:38:10 CET 2013


Hi Zeromq folks,

I have been trying to use pgm. I went through the guide but couldnt find a
complete example I went through the zeromq mailinglist and found something
like this. But I'm unable to get it to work. I'm starting the publisher and
subscriber on the same machine.


Publisher :

#include <zmq.h>
#include <string>
#include <unistd.h>
using namespace std;

int main(int argc, char* argv[])
{
    void* context = zmq_init(1);
    void* publisher = zmq_socket(context,ZMQ_PUB);
    zmq_bind(publisher,"epgm://eth0;239.192.1.1:5555");

    string s = "x.";

    while(1)
    {
    zmq_msg_t message;
    zmq_msg_init_size (&message, s.size());
    memcpy (zmq_msg_data (&message), s.c_str(), s.size());
    int rc = zmq_sendmsg (publisher, &message, ZMQ_DONTWAIT);
    }

    sleep(3);
}

Subscriber :

#include <zmq.h>
#include <string>
#include <unistd.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
    void* context = zmq_init(1);
    void* subscriber = zmq_socket(context,ZMQ_SUB);
    string subscription = "x";
    zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, subscription.c_str(),
subscription.size());
    zmq_connect(subscriber,"epgm://eth0;239.192.1.1:5555");
    int ret_val;
    while(1)
    {
        string msg_buffer;
        zmq_msg_t message;
        zmq_msg_init(&message);
        if((ret_val = zmq_recvmsg (subscriber, &message, ZMQ_DONTWAIT)) < 0
)
        {
            cout<<"Not receiving"<<endl;
            sleep(1);
            continue;

        }
        int size = zmq_msg_size (&message);
        msg_buffer.assign((const char*)zmq_msg_data(&message),size);
        cout<<"Msg buffer : "<<msg_buffer<<endl;

    }
    sleep(3);
}

Compilation :

g++ -I../zeromq-3.2.2/include ./zmq_pub.cpp
../zeromq-3.2.2/src/.libs/libzmq.a -lpthread -lrt -o zmq_pub
g++ -I../zeromq-3.2.2/include ./zmq_sub.cpp
../zeromq-3.2.2/src/.libs/libzmq.a -lpthread -lrt -o zmq_sub
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130318/bb487eea/attachment.htm>


More information about the zeromq-dev mailing list