Question

I am learning to use zeromq polling in android . I am polling on a req socket and a sub socket in the android program(client). So that this client can receive both reply messages from the server and also published messages.

My polling is not working. Both the req socket and the publish socket does not get polled in. If i don't use polling both the sockets receive the message.

I tried searching online but could not find anything relevant. The client code is this :

    public void run()
   {
    ZMQ.Context context = ZMQ.context(1);
    ZMQ.Socket reqsocket = context.socket(ZMQ.REQ);
    ZMQ.Socket subsocket =context.socket(ZMQ.SUB);
    reqsocket.connect("tcp://10.186.3.174:8081");
    subsocket.connect("tcp://10.186.3.174:8083");
    subsocket.subscribe("".getBytes());
    byte[] receivedmessage;
    Poller poller=context.poller();
    poller.register(reqsocket,Poller.POLLIN);
    poller.register(subsocket,Poller.POLLIN);

    reqsocket.send(msg.getBytes(),0); 

    while(!Thread.currentThread().isInterrupted())
     {

        if(poller.pollin(0))
        {
            receivedmessage=s.recv(0);

        }
          if(poller.pollin(0))
          {
            receivedmessage=subsocket.recv(0);

          }
   }
    s.close();
    context.term();

}

Am i missing out something or doing something wrong?

Was it helpful?

Solution

It looks like there are 3 problems with this. The main one is you need to call poller.poll() as the first thing inside the while loop. This is why you are not getting any messages.

The next issue is that you're checking the same index for both sockets: I expect the second if statement needs to be

if(poller.pollin(1))

Lastly, the req socket requires a send before every receive, so the call to send needs to be inside the while loop, and before the poller.poll() you just added above :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top