[zeromq-dev] How to get transactions from bitcoind?

Marcin Romaszewicz marcin at brkt.com
Thu Jan 12 22:37:49 CET 2017


If it's blocked on socket.recv() it means you're not receiving anything,
because it will wait until the first message arrives. If you want to
receive stuff and print in a loop like the python code, you need to do it
roughly like this (I'm just typing in from memory, so there might be some
compiler errors here). Message can have lots parts, and in the C API, you
need to read all the parts, since there's no recv_multipart.

ZMQ subscriptions can also miss pub messages for a little while after
connected, so make sure your publisher is sending more than one thing.

while true {
  std::vector<std::string> msg_parts
  // Receive all message parts
  do {
     zmq::message_t msg_part;
     bool status = socket.recv(&msg_part, 0)
     // You should do error checking here.

     // Read data from msg_part, and stick it into a std::string then put
that into the part list
     msg_parts.push_back(std::string(static_cast<char*>(msg_part.data()),
msg_part.size())
  } while (socket.getsockopt(ZMQ_RCVMORE))

  // msg_parts now contains all parts of the message.
  fprintf(stdout, "Received %d message parts\n", msg_parts.size())
}

On Thu, Jan 12, 2017 at 12:57 AM, Andy <borucki.andrzej at gmail.com> wrote:

> Python version is here: https://github.com/bitcoin/bitcoin/blob/master/
> contrib/zmq/zmq_sub.py
> I try to write C++ version using zeroMQ and wrapper https://github.com/
> alanw/zmqcpp
>
> #include <stdexcept>
> #include <iostream>
> #include <cstdlib>
> #include <cstring>
>
> #include "zmq.hpp"
>
> int main(int argc, char **argv) {
>     zmq::context_t context(1);
>     zmq::socket_t socket(context, ZMQ_SUB);
>     socket.setsockopt(ZMQ_SUBSCRIBE, "hashblock");
>     socket.setsockopt(ZMQ_SUBSCRIBE, "hashtx");
>     socket.setsockopt(ZMQ_SUBSCRIBE, "rawblock");
>     socket.setsockopt(ZMQ_SUBSCRIBE, "rawtx");
>     socket.connect("tcp://127.0.0.1:28332");
>     zmq::message_t msg;
> //    char buf[80];
>     socket.recv(&msg,0); //<--here is blocked !
>     return 0;
> }
>
> Code above is similar to Python code, but I don;t know how to receive data to console
>
> Thanks,
>
>
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> https://lists.zeromq.org/mailman/listinfo/zeromq-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20170112/9a788b0b/attachment.htm>


More information about the zeromq-dev mailing list