ZMQ Hello world不起作用

时间:2017-09-08 09:35:54

标签: c++ sockets networking network-programming zeromq

我尝试运行简单的ZMQ应用程序(ROUTER / DEALER)。

我只是将DEALER的请求发送给ROUTER,然后将其发回。但是DEALER无法接收它。

我在一个进程中运行它(ROUTER有自己的线程)。

#include <zmq.hpp>
#include <string>
#include <iostream>
#include <thread>

void router()
{
    zmq::context_t context(1);
    zmq::socket_t socket(context, ZMQ_ROUTER);
    socket.bind("tcp://*:5561");

    while(1)
    {
        //  Wait for next request from client
        zmq::message_t reply;
        socket.recv (&reply);

        std::cout << "Router: Received request" << std::endl;

        //  Send reply back to client
        std::string string= "example";
        zmq::message_t message(string.size());
        memcpy (message.data(), string.data(), string.size());

        std::cout << "Router: Sending" << std::endl;
        socket.send (message);
    }
}

int main ()
{
    std::thread t{&router};

    //  Prepare our context and socket
    zmq::context_t context (2);
    zmq::socket_t socket (context, ZMQ_DEALER);

    std::cout << "Dealer: Connecting to hello world server…" << std::endl;
    socket.connect ("tcp://127.0.0.1:5561");

    for (int i = 0; i != 10; i++)
    {
        zmq::message_t request (5);
        memcpy (request.data (), "Hello", 5);
        std::cout << "Dealer: Sending Hello " << i << "…" << std::endl;
        socket.send (request);

        zmq::message_t reply;
        socket.recv (&reply);
        std::cout << "Dealer: Received " << i << std::endl;
    }
    return 0;
}

我有一个输出:

Dealer: Connecting to hello world server…
Dealer: Sending Hello 0…
Router: Received request
Router: Sending
Router: Received request
Router: Sending

1 个答案:

答案 0 :(得分:1)

来自socket上的ZMQ's documentation

  

当接收消息时,ZMQ_ROUTER套接字应该预先添加消息   包含消息的始发对等体的标识的部分   在将它传递给应用程序之前。收到的消息是   从所有相互关联的同行中排队等候。发送消息时   ZMQ_ROUTER套接字应删除消息的第一部分并使用   它确定消息应被路由的对等体的身份   到。

所以将代码修改为:

#include <zmq.hpp>
#include <string>
#include <iostream>
#include <thread>
#include <unistd.h>
void router()
{
    zmq::context_t context(1);
    zmq::socket_t socket(context, ZMQ_ROUTER);
    socket.bind("tcp://*:5561");

    while(1) // Fix that infinite loop or your thread won't join
    {
        //  Wait for next request from client
        zmq::message_t id;
        socket.recv (&id);

        zmq::message_t reply;
        socket.recv (&reply);
        std::cout << std::string(static_cast<char*>(reply.data()),reply.size()) << std::endl;
        std::cout << "Router: Received request" << std::endl;

        //  Send reply back to client
        zmq::message_t copy_id;
        copy_id.copy(&id);
        std::string string= "example";
        zmq::message_t message(string.size());
        memcpy (message.data(), string.data(), string.size());
        std::cout << "Router: Sending" << std::endl;
        socket.send(id, ZMQ_SNDMORE);
        socket.send(message);
    }
    sleep(1);
    socket.setsockopt(ZMQ_LINGER, 0);
    socket.close();
    context.close();
}

int main ()
{
    std::thread t{&router};

    //  Prepare our context and socket
    zmq::context_t context (2);
    zmq::socket_t socket (context, ZMQ_DEALER);

    std::cout << "Dealer: Connecting to hello world server…" << std::endl;
    socket.connect ("tcp://127.0.0.1:5561");

    for (int i = 0; i != 10; i++)
    {
        zmq::message_t request (5);
        memcpy (request.data (), "Hello", 5);
        std::cout << "Dealer: Sending Hello " << i << "…" << std::endl;
        socket.send(request);

        zmq::message_t reply;
        socket.recv(&reply);
        std::cout << "Dealer: Received " << i << std::endl;
    }
    socket.setsockopt(ZMQ_LINGER, 0);
    socket.close();
    context.close();
    t.join();
    return 0;
}