Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ZeroMQ poll throws an error: "socket operation on non-socket"?

Tags:

c++

zeromq

I encounter a serious and odd issue with ZeroMQ and its C++ binding: each time I perform a polling operation on zmq socket, it throws an error of:

socket operation on non-socket

Below is the code to reproduce it:

#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.
  }
}
like image 389
WintermeW Avatar asked Oct 21 '25 06:10

WintermeW


1 Answers

Given ZeroMQ API / C++ binding documentation recommends to do so, one ought follow a practice to:

To obtain a ØMQ socket for use in a zmq_pollitem_t structure, you should cast an instance of the socket_t class to (void *).

like image 104
user3666197 Avatar answered Oct 22 '25 21:10

user3666197



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!