/**************************************************************
技术博客
http://www.cnblogs.com/itdef/
技术交流群
群号码:324164944
欢迎c c++ windows驱动爱好者 服务器程序员沟通交流
**************************************************************/
//test1.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<zhelpers.hpp>#include<thread>voidRouterfunc () {zmq::context_t context (1); zmq::socket_t Broker (context, zmq_router); Broker.bind ("tcp://*:5671"); while(1) {std::stringIdentity =S_RECV (Broker); STD::stringRecvstr =S_RECV (Broker); Recvstr=S_RECV (Broker); Std::cout<< Recvstr <<Std::endl; S_sendmore (broker, identity); S_sendmore (Broker,""); S_send (Broker,"reply1"); Identity=S_RECV (Broker); Recvstr=S_RECV (Broker); Recvstr=S_RECV (Broker); Std::cout<< Recvstr <<Std::endl; S_sendmore (broker, identity); S_sendmore (Broker,""); S_send (Broker,"Reply2"); }}voidDealerfunc () {zmq::context_t context (1); zmq::socket_t Worker (context, zmq_dealer); Worker.connect ("tcp://localhost:5671"); while(1) {S_sendmore (worker,""); S_send (worker,"Hello world1"); STD::stringRecvstr =s_recv (worker); Recvstr=s_recv (worker); Std::cout<< Recvstr <<Std::endl; S_sendmore (worker,""); S_send (worker,"Hello world2"); Recvstr=s_recv (worker); Recvstr=s_recv (worker); Std::cout<< Recvstr <<Std::endl; }}voidTestrouterdealer () {std::thread Routerthread=Std::thread (Routerfunc); Std::thread Dealerthread=Std::thread (Dealerfunc); Routerthread.join (); Dealerthread.join (); return;}voidRouterfun () {zmq::context_t context (1); zmq::socket_t Broker (context, zmq_router); Broker.bind ("tcp://*:5671"); while(1) {std::stringIdentity =S_RECV (Broker); STD::stringRecvstr =S_RECV (Broker); Recvstr=S_RECV (Broker); Std::cout<< Recvstr <<Std::endl; S_sendmore (broker, identity); S_sendmore (Broker,""); S_send (Broker,"Work Harder1"); Identity=S_RECV (Broker); Recvstr=S_RECV (Broker); Recvstr=S_RECV (Broker); Std::cout<< Recvstr <<Std::endl; S_sendmore (broker, identity); S_sendmore (Broker,""); S_send (Broker,"Work Harder2"); }}voidReqfun () {zmq::context_t context (1); zmq::socket_t Worker (context, zmq_req); S_SET_ID (worker, (intptr_t)3); Worker.connect ("tcp://localhost:5671"); while(1) {s_send (worker,"Hi Boss1"); STD::stringRecvstr =s_recv (worker); Std::cout<< Recvstr <<Std::endl; S_send (worker,"Hi Boss2"); Recvstr=s_recv (worker); Std::cout<< Recvstr <<Std::endl; }}voidTestrouterreq () {std::thread Routerthread=Std::thread (Routerfun); Std::thread Reqthread=Std::thread (Reqfun); Routerthread.join (); Reqthread.join (); return;}intMain () {//Testrouterdealer ();Testrouterreq (); return 0;}
View Code
ZEROMQ Learning Record (ix) Contact Code Learning Zmq_router Zmq_readler