ZMQ Hello world 不工作

ZMQ Hello world does not work

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

我只是从 DEALERROUTER 发送了一个请求,然后将其发回。但是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

来自套接字上的 ZMQ's documentation :

When receiving messages a ZMQ_ROUTER socket shall prepend a message part containing the identity of the originating peer to the message before passing it to the application. Messages received are fair-queued from among all connected peers. When sending messages a ZMQ_ROUTER socket shall remove the first part of the message and use it to determine the identity of the peer the message shall be routed to.

因此将您的代码修改为如下内容:

#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;
}