Skip to content

Instantly share code, notes, and snippets.

@andr1972
Created January 13, 2017 15:40
Show Gist options
  • Save andr1972/b72ecf68ea489d0a457041af2fd12a60 to your computer and use it in GitHub Desktop.
Save andr1972/b72ecf68ea489d0a457041af2fd12a60 to your computer and use it in GitHub Desktop.
/*
using: http://zeromq.org/
see https://github.com/bitcoin/bitcoin/blob/master/contrib/zmq/zmq_sub.py
first call: bitcoind -zmqpubhashtx=tcp://127.0.0.1:28332 -zmqpubrawtx=tcp://127.0.0.1:28332 &
*/
#include <stdexcept>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "zmq.hpp"//from https://github.com/alanw/zmqcpp
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");
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<bool>(ZMQ_RCVMORE));
// msg_parts now contains all parts of the message.
fprintf(stdout, "Received %d message parts\n", msg_parts.size());
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment