Created
January 5, 2019 21:55
-
-
Save trya/424f7f16ed89561ad67c537be89e320c to your computer and use it in GitHub Desktop.
ZMQ subscriber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "zmq.hpp" | |
static const char *addr = "tcp://localhost:1337"; | |
void sub_loop(void) | |
{ | |
zmq::context_t ctx; | |
zmq::socket_t so(ctx, ZMQ_SUB); | |
so.connect(addr); | |
std::cerr << "zmq socket connected to " << addr << std::endl; | |
/* subscribe to everything */ | |
so.setsockopt(ZMQ_SUBSCRIBE, nullptr, 0); | |
std::cerr << "socket subscribed to everything" << std::endl; | |
while (true) { | |
zmq::message_t msg; | |
so.recv(&msg); | |
std::cout.write(static_cast<const char *>(msg.data()), msg.size()); | |
std::cout.flush(); | |
if (!std::cout.good()) break; | |
} | |
if (!std::cout) { | |
exit(EXIT_FAILURE); | |
} | |
} | |
int main(int argc, char **argv) | |
{ | |
if (argc > 2) { | |
std::cerr << "usage: " << argv[0] << " [endpoint_url]" << std::endl; | |
exit(EXIT_FAILURE); | |
} else if (argc == 2) { | |
addr = argv[1]; | |
} | |
try { | |
sub_loop(); | |
} catch (zmq::error_t &err) { | |
std::cerr << "zmq error: " << err.what() << std::endl; | |
exit(EXIT_FAILURE); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment