简体   繁体   中英

ZeroMQ Poll - Resource temporarily unavailable

I am trying to run a small ZeroMQ application.
I am working on an Ubuntu machine with also a QT framework used in my code.
First of all, I create a test_class . This class creates a socket and a context, than establishes a connection.
Here is my test class :

Header File :

class test_class
{
public:
    test_class();
    ~test_class();

    zmq::socket_t* get_conn_socket() const;
    void establish_connection();

private:
    zmq::context_t * m_context;
    zmq::socket_t * m_socket;
};

Source File :

#include "test_class.h"

test_class::test_class()
{
    m_context = new zmq::context_t(1);
    m_socket = new zmq::socket_t(*m_context, ZMQ_DEALER);
}

test_class::~test_class()
{

}

zmq::socket_t* test_class::get_conn_socket() const
{
    return m_socket;
}

void test_class::establish_connection()
{
    m_socket->connect("tcp://localhost:9999");
}

In the main code, I create a test_class instance and establish a connection, using this instance. Then wait for some message from the broker.

qDebug() << "TEST CODE WORKING";

int mtime = 1000;
test_class *test = new test_class();
test->establish_connection();

s_send(*(test->get_conn_socket()), "READY");

while(true)
{
    zmq::socket_t* temp_socket = test->get_conn_socket();
    zmq::pollitem_t items[] = { { *temp_socket, 0, ZMQ_POLLIN, 0} };
    if( zmq_poll(items, 1, mtime) > 0)
    {

        qDebug() << "ZMQ_POLL_IN";

        if ( items[0].revents & ZMQ_POLLIN )
        {
            zmsg msg(*temp_socket);

            if(strcmp(msg.body(), "HEARTBEAT") == 0)
            {
                qDebug() << "HEARTBEAT message";
            }
            else
            {
                qDebug() << "SOME OTHER message";
            }
        }
    }
    else
    {
        qDebug() << zmq_strerror(errno);
    }


    s_send(*temp_socket, "HEARTBEAT");

}

Finally,
my test_class output is like this :

Resource temporarily unavailable 
Resource temporarily unavailable 
Resource temporarily unavailable 
Resource temporarily unavailable 
Resource temporarily unavailable 
Resource temporarily unavailable 
Resource temporarily unavailable 
Resource temporarily unavailable 
Resource temporarily unavailable 

And broker_code output is:

HEARTBEAT MESSAGE COME FROM WORKER
HEARTBEAT MESSAGE COME FROM WORKER
HEARTBEAT MESSAGE COME FROM WORKER
HEARTBEAT MESSAGE COME FROM WORKER
HEARTBEAT MESSAGE COME FROM WORKER
HEARTBEAT MESSAGE COME FROM WORKER
HEARTBEAT MESSAGE COME FROM WORKER

When I check the communication between the broker code and my code, using a tcpdump, the tcpdump's output is like this :

19:58:37.815283 IP localhost.9999 > localhost.39278: Flags [.], ack 33, win 342, options [nop,nop,TS val 3575439312 ecr 3575439312], length 0
19:58:38.816456 IP localhost.39278 > localhost.9999: Flags [P.], seq 33:44, ack 1, win 342, options [nop,nop,TS val 3575440313 ecr 3575439312], length 11
19:58:38.816475 IP localhost.9999 > localhost.39278: Flags [.], ack 44, win 342, options [nop,nop,TS val 3575440313 ecr 3575440313], length 0
19:58:39.817500 IP localhost.39278 > localhost.9999: Flags [P.], seq 44:55, ack 1, win 342, options [nop,nop,TS val 3575441315 ecr 3575440313], length 11

So, seems like the broker sends me a message. But why my test_class does not recognize a message ? Why "Resource temporarily unavailable" error rised ?

In recent versions of ZeroMQ, the API documentation explicitly recommends to type-cast the ZeroMQ socket using (void *)

zmq::pollitem_t items[] = { { (void *) temp_socket, // void *ZeroMQ Socket
                                                 0, // int   <fd>
                                        ZMQ_POLLIN, // short  EVENTS
                                                 0  // short REVENTS
                              }
                            };

so as to be correctly "seated" and used inside a zmq_pollitem_t structure, so do not hesitate to go for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM