zeromq学习记录联系代码学习ZMQ_ROUTER ZMQ_READLER
Posted itdef
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zeromq学习记录联系代码学习ZMQ_ROUTER ZMQ_READLER相关的知识,希望对你有一定的参考价值。
/**************************************************************
技术博客
http://www.cnblogs.com/itdef/
技术交流群
群号码:324164944
欢迎c c++ windows驱动爱好者 服务器程序员沟通交流
**************************************************************/
// test1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <zhelpers.hpp> #include <thread> void RouterFunc() { zmq::context_t context(1); zmq::socket_t broker(context, ZMQ_ROUTER); broker.bind("tcp://*:5671"); while (1) { std::string identity = s_recv(broker); std::string recvStr = 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"); } } void DealerFunc() { 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::string recvStr = 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; } } void TestRouterDealer() { std::thread routerThread = std::thread(RouterFunc); std::thread dealerThread = std::thread(DealerFunc); routerThread.join(); dealerThread.join(); return; } void RouterFun() { zmq::context_t context(1); zmq::socket_t broker(context, ZMQ_ROUTER); broker.bind("tcp://*:5671"); while (1) { std::string identity = s_recv(broker); std::string recvStr = 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"); } } void ReqFun() { 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::string recvStr = s_recv(worker); std::cout << recvStr << std::endl; s_send(worker, "Hi Boss2"); recvStr = s_recv(worker); std::cout << recvStr << std::endl; } } void TestRouterReQ() { std::thread routerThread = std::thread(RouterFun); std::thread reqThread = std::thread(ReqFun); routerThread.join(); reqThread.join(); return; } int main() { //TestRouterDealer(); TestRouterReQ(); return 0; }
以上是关于zeromq学习记录联系代码学习ZMQ_ROUTER ZMQ_READLER的主要内容,如果未能解决你的问题,请参考以下文章