繼 學習代碼 Multiple socket reader in C++

Multiple socket reader in C++

//
//Reading from multiple sockets in C++
//This version uses a simple recv loop
//
// Olivier Chamoux <[email protected]>

#include "zhelpers.hpp"

int main (int argc,char *argv[])
{
   //Prepare our context and sockets
    zmq::context_t context(1);

  //Connect to task ventilator
   zmq::socket_t receiver(context, ZMQ_PULL);
   receiver.connect("tcp://localhost:5557");

  //Connect to weather server
   zmq::socket_t subscriber(context, ZMQ_SUB);
   subscriber.connect("tcp://localhost:5556");
   subscriber.setsockopt(ZMQ_SUBSCRIBE,"10001 ", 6);

  //Process messages from both sockets
  //We prioritize traffic from the task ventilator
   while (1) {

    //Process any waiting tasks
      bool rc;
       do {
                        zmq::message_t task;
                        if ((rc= receiver.recv(&task, ZMQ_DONTWAIT))== true) {
                        //process task
       }
     } while(rc == true);

      //Process any waiting weather updates
     do {
             zmq::message_t update;
             if ((rc= subscriber.recv(&update, ZMQ_DONTWAIT))== true) {
            //process weather update

     }
    } while(rc == true);

      //No activity, so sleep for 1 msec
      s_sleep(1);
   }
     return0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章