我遇到了ZeroMQ及其C ++绑定的严重问题:每次在zmq套接字上执行轮询操作时,都会抛出错误:
socket operation on non-socket
以下是重现它的代码:
#include <zmq.hpp>
int main() {
zmq::context_t ctx(1);
zmq::socket_t sock1( ctx, ZMQ_REQ );
zmq::socket_t sock2( ctx, ZMQ_PUB );
sock1.bind( "tcp://*:10001" );
sock2.bind( "tcp://*:10002" );
zmq::pollitem_t* items = ( zmq::pollitem_t* ) malloc( 2 * sizeof( zmq::pollitem_t ) );
items[0] = { &sock1, 0, ZMQ_POLLIN, 0 };
items[1] = { &sock2, 0, ZMQ_POLLIN, 0 };
while(1) {
zmq::poll ( items, 2, 500 ); // Throws error !! socket operation on non-socket.
}
}
答案 0 :(得分:2)
鉴于ZeroMQ API / C ++绑定文档建议这样做,应该按照惯例进行:
要获得用于
zmq_pollitem_t
结构的ØMQ套接字,您应该将socket_t
类的实例强制转换为(void *)
强>