ZMQ Dealer向自己发送和接收消息

时间:2019-05-31 00:16:41

标签: c++ zeromq

我有一个zmq经销商插座,该插座正在连接到我的机器上没有其他人连接的端口。我使用ZMQ_IMMEDIATE选项阻止套接字在没有连接对等方的情况下发送消息。如果我尝试发送一条消息足够多的时间(大约五分钟),那么最终我将能够发送该消息,并接收到一条消息,其中包含我尝试发送的大小和数据。

使用zmq_socket_monitor最终表明,发销商套接字能够连接并向其发送消息或从其接收消息。谁能解释为什么会这样?

#include <unistd.h>
#include <iostream>
#include <zmq.hpp>
#include <thread>
#include <sstream>
static int read_msg(void* s, zmq_event_t* event, char* ep)
{
 int rc ;
 zmq_msg_t msg1; // binary part
 zmq_msg_init (&msg1);
 zmq_msg_t msg2; // address part
 zmq_msg_init (&msg2);
 rc = zmq_msg_recv (&msg1, s, 0);
 if (rc == -1 && zmq_errno() == ETERM)
 return 1 ;
 assert (rc != -1);
 assert (zmq_msg_more(&msg1) != 0);
 rc = zmq_msg_recv (&msg2, s, 0);
 if (rc == -1 && zmq_errno() == ETERM)
 return 1;
 assert (rc != -1);
 assert (zmq_msg_more(&msg2) == 0);
 // copy binary data to event struct
 const char* data = (char*)zmq_msg_data(&msg1);
 memcpy(&(event->event), data, sizeof(event->event));
 memcpy(&(event->value), data+sizeof(event->event), sizeof(event->value));
 // copy address part
 const size_t len = zmq_msg_size(&msg2) ;
 ep = (char*)memcpy(ep, zmq_msg_data(&msg2), len);
 *(ep + len) = 0 ;
 return 0 ;
}


// dealer socket monitor thread
static void *dealer_socket_monitor (void *ctx)
{
 zmq_event_t event;
 static char addr[1025] ;
 int rc;

 printf("starting monitor...\n");

 void *s = zmq_socket (ctx, ZMQ_PAIR);
 assert (s);
 int num = 0;
 rc = zmq_connect (s, "inproc://monitor.dealer");
 assert (rc == 0);
 while (!read_msg(s, &event, addr)) {
 switch (event.event) {
 case ZMQ_EVENT_CONNECT_RETRIED:
 printf (" DEALER connect retried socket descriptor %d %d\n", event.value, num);
 printf (" DEALER connect retried socket address %s\n", addr);
 break;
 case ZMQ_EVENT_CONNECT_DELAYED:
 printf (" DEALER connect delayed socket descriptor %d %d\n", event.value, num);
 printf (" DEALER connect delayed socket address %s\n", addr);
 break;
 case ZMQ_EVENT_LISTENING:
 printf (" DEALER listening socket descriptor %d %d\n", event.value, num);
 printf (" DEALER listening socket address %s\n", addr);
 break;
 case ZMQ_EVENT_CONNECTED:
 printf (" DEALER connected socket descriptor %d %d\n", event.value, num);
 printf (" DEALER connected socket address %s\n", addr);
 break;
 case ZMQ_EVENT_MONITOR_STOPPED:
 printf (" DEALER monitor ending socket descriptor %d %d\n", event.value, num);
 printf (" DEALER monitor ending socket address %s\n", addr);
 break;
 case ZMQ_EVENT_ACCEPTED:
 printf (" DEALER accepted socket descriptor %d %d\n", event.value, num);
 printf (" DEALER accepted socket address %s\n", addr);
 break;
 case ZMQ_EVENT_CLOSE_FAILED:
 printf (" DEALER socket close failure error code %d %d\n", event.value, num);
 printf (" DEALER socket address %s\n", addr);
 break;
 case ZMQ_EVENT_CLOSED:
 printf (" DEALER closed socket descriptor %d %d\n", event.value, num);
 printf (" DEALER closed socket address %s\n", addr);
 break;
 case ZMQ_EVENT_DISCONNECTED:
 printf (" DEALER disconnected socket descriptor %d %d\n", event.value, num);
 printf (" DEALER disconnected socket address %s\n", addr);
 break;
 default:
 printf(" DEALER default case\n");
 }
 }
 zmq_close (s);
 return NULL;
}
int main() {
     pthread_t thread ;
    std::string connect = "tcp://localhost:49150";
    zmq::context_t context(1);
    zmq::socket_t client (context, ZMQ_DEALER);
    int m_linger = 0;
    int count = 0;
    int m_timeoutMs = 1000;
    int imm = 1;
    // release the socket / port asa it disconnects
    client.setsockopt(ZMQ_LINGER, &m_linger, sizeof(m_linger));
    client.setsockopt(ZMQ_RCVTIMEO, &m_timeoutMs, sizeof(m_timeoutMs));
    client.setsockopt(ZMQ_SNDTIMEO, &m_timeoutMs, sizeof(m_timeoutMs));
    client.setsockopt(ZMQ_IMMEDIATE, &imm, sizeof(imm));

    client.connect(connect.c_str());
    std::cout << "Connecting client to " << connect << std::endl;

     // socket monitoring
    int rc = zmq_socket_monitor ((void*)client, "inproc://monitor.dealer", ZMQ_EVENT_ALL);
    rc = pthread_create (&thread, NULL, dealer_socket_monitor, (void*)context);
    int more = 0;
    while(true)
    {
        zmq::message_t blank{};
        zmq::message_t message(sizeof(int));
        zmq::message_t response;
        int more; //  Multipart detection
        std::cout << "Client no linger sending message more enabled" << std::endl;
        auto ret = client.send(blank, ZMQ_SNDMORE);
        if (!ret)
        {
            std::cout << "timeout on send, trying again" << std::endl;
            continue;
        }
        std::cout << "Client no linger sending message more disabled" << std::endl;
        ret = client.send(message);
        if (!ret)
        {
            std::cout << "timeout on send second, trying again";
            continue;
        }
        ret = client.recv(&response);
        size_t more_size = sizeof (more);
        client.getsockopt(ZMQ_RCVMORE, &more, &more_size);
        while (more)
        {
            std::cout << "Client received message with more coming. Size of message " << response.size() << std::endl;
            client.recv(&response);
            client.getsockopt(ZMQ_RCVMORE, &more, &more_size);
        }
        std::cout << "Client received final part of message. Size of message " << response.size() << " message " << *(reinterpret_cast<int*>(message.data())) << std::endl;
        exit(0);
    }
}

请注意,消息的大小是整数的大小,这是我初始化zmq消息所用的大小。查看我最初创建此错误的程序,该消息包含尝试发送的相同数据。 我从运行程序得到的输出结束:

 DEALER closed socket descriptor 10 0
 DEALER closed socket address tcp://localhost:49150
 DEALER connect retried socket descriptor 185 0
 DEALER connect retried socket address tcp://localhost:49150
 DEALER connect delayed socket descriptor 115 0
 DEALER connect delayed socket address tcp://localhost:49150
 DEALER connected socket descriptor 10 0
 DEALER connected socket address tcp://localhost:49150
Client no linger sending message more disabled
Client received message with more coming. Size of message 0
Client received final part of message. Size of message 4 message 0

由GCC 6.3编译:g ++ -std = c ++ 11 client.cpp -o client -g -lzmq -lpthread cppzmq-devel 4.1.4-6和zmq 4.1.4-6

1 个答案:

答案 0 :(得分:4)

我在NodeJS上有一个similar issue,套接字最终可以与其自身连接。这里似乎发生了同样的事情:ZeroMQ套接字创建一个本地临时端口,该端口与您要连接的端口(49150)相同。

如果是这样,那么当您连接到没有服务正在监听的保留端口(<1024)(例如端口7)时,应该不会发生这种情况。绑定/侦听临时端口范围之外的端口(在Linux上为<32768)解决问题。

使用诸如netstattcpdump / wireshark之类的工具来证明这实际上是在套接字/网络级别上发生的。