码迷,mamicode.com
首页 > 其他好文 > 详细

zeromq学习记录(九)联系代码学习ZMQ_ROUTER ZMQ_READLER

时间:2016-04-04 19:46:31      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

/**************************************************************
技术博客
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;
}
View Code

 技术分享

 

技术分享

zeromq学习记录(九)联系代码学习ZMQ_ROUTER ZMQ_READLER

标签:

原文地址:http://www.cnblogs.com/itdef/p/5352487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!