// Broker
int main(int argc, char* argv[]) {
void* ctx = zmq_ctx_new(); assert(ctx);
void* frontend = zmq_socket(ctx, ZMQ_XSUB); assert(frontend); void* backend = zmq_socket(ctx, ZMQ_XPUB); assert(backend);
int rc = zmq_bind(frontend, "tcp://:5570"); assert(rc==0); rc = zmq_bind(backend, "tcp://:5571"); assert(rc==0);
zmq_proxy_steerable(frontend, backend, nullptr, nullptr);
zmq_close(frontend); zmq_close(backend);
rc = zmq_ctx_term(ctx); return 0; }
// PUB
using namespace std; using namespace chrono;
int main(int argc, char* argv[]) { void* context = zmq_ctx_new(); assert (context); /* Create a ZMQ_SUB socket / void *socket = zmq_socket (context, ZMQ_PUB); assert (socket); / Connect it to the host
localhost, port 5571 using a TCP transport */ int rc = zmq_connect (socket, "tcp://localhost:5570"); assert (rc == 0);
while (true) { int len = zmq_send(socket, "hello", 5, 0); cout << "pub len = " << len << endl; this_thread::sleep_for(milliseconds(1000)); } }
// SUB
using namespace std;
int main(int argc, char* argv[]) { void* context = zmq_ctx_new(); assert (context); /* Create a ZMQ_SUB socket / void *socket = zmq_socket (context, ZMQ_SUB); assert (socket); / Connect it to the host localhost, port 5571 using a TCP transport */ int rc = zmq_connect (socket, "tcp://localhost:5571"); assert (rc == 0); rc = zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "", 0); assert (rc == 0);
while (true) { char buffer[1024] = {0}; int len = zmq_recv(socket, buffer, sizeof(buffer), 0); cout << "len = " << len << endl; cout << "buffer = " << buffer << endl; } }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
// Broker
include
int main(int argc, char* argv[]) {
void* ctx = zmq_ctx_new();
assert(ctx);
void* frontend = zmq_socket(ctx, ZMQ_XSUB);
assert(frontend);
void* backend = zmq_socket(ctx, ZMQ_XPUB);
assert(backend);
int rc = zmq_bind(frontend, "tcp://:5570");
assert(rc==0);
rc = zmq_bind(backend, "tcp://:5571");
assert(rc==0);
zmq_proxy_steerable(frontend, backend, nullptr, nullptr);
zmq_close(frontend);
zmq_close(backend);
rc = zmq_ctx_term(ctx);
return 0;
}
// PUB
include
include
using namespace std;
using namespace chrono;
int main(int argc, char* argv[])
{
void* context = zmq_ctx_new();
assert (context);
/* Create a ZMQ_SUB socket /
void *socket = zmq_socket (context, ZMQ_PUB);
assert (socket);
/ Connect it to the host
localhost, port 5571 using a TCP transport */
int rc = zmq_connect (socket, "tcp://localhost:5570");
assert (rc == 0);
while (true)
{
int len = zmq_send(socket, "hello", 5, 0);
cout << "pub len = " << len << endl;
this_thread::sleep_for(milliseconds(1000));
}
}
// SUB
include
include
using namespace std;
int main(int argc, char* argv[])
{
void* context = zmq_ctx_new();
assert (context);
/* Create a ZMQ_SUB socket /
void *socket = zmq_socket (context, ZMQ_SUB);
assert (socket);
/ Connect it to the host localhost, port 5571 using a TCP transport */
int rc = zmq_connect (socket, "tcp://localhost:5571");
assert (rc == 0);
rc = zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "", 0);
assert (rc == 0);
while (true)
{
char buffer[1024] = {0};
int len = zmq_recv(socket, buffer, sizeof(buffer), 0);
cout << "len = " << len << endl;
cout << "buffer = " << buffer << endl;
}
}