[英]ZeroMQ Poll - Resource temporarily unavailable
I am trying to run a small ZeroMQ application. 我正在尝试运行一个小的ZeroMQ应用程序。
I am working on an Ubuntu machine with also a QT framework used in my code. 我正在使用在我的代码中使用QT框架的Ubuntu计算机上工作 。
First of all, I create a test_class
. 首先,我创建一个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. 在主代码中,我创建一个test_class
实例并使用该实例建立连接。 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 : 我的test_class
输出是这样的:
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: 和broker_code
输出是:
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 : 当我使用tcpdump检查代理代码与我的代码之间的通信时,tcpdump的输出如下所示:
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 ? 但是,为什么我的test_class
无法识别消息? Why "Resource temporarily unavailable" error rised ? 为什么出现“资源暂时不可用”错误?
In recent versions of ZeroMQ, the API documentation explicitly recommends to type-cast the ZeroMQ socket using (void *)
在最新版本的ZeroMQ中,API文档明确建议使用(void *)
强制转换ZeroMQ套接字类型。
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. 以便正确地“安置”并在zmq_pollitem_t
结构中使用,因此请不要犹豫。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.