我正在尝试运行一个小型的ZeroMQ应用程序
我正在使用Ubuntu计算机,还有我的代码中使用的QT框架
首先,我创建了一个test_class
。此类创建套接字和上下文,而不是建立连接
这是我的测试类:
标题文件:
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;
};
源文件:
#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");
}
在主代码中,我使用此实例创建一个test_class
实例并建立连接。然后等待来自经纪人的一些消息。
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");
}
最后,
我的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
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
当我使用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
所以,似乎经纪人给我发了一条消息。但为什么我的test_class
无法识别消息?为什么&#34;资源暂时不可用&#34; 错误上升?
答案 0 :(得分:0)
在ZeroMQ的最新版本中,API文档明确建议使用 StatusCode
(void *)
以便正确地坐着&#34;并在 zmq::pollitem_t items[] = { { (void *) temp_socket, // void *ZeroMQ Socket
0, // int <fd>
ZMQ_POLLIN, // short EVENTS
0 // short REVENTS
}
};
结构中使用,所以请不要犹豫。