Skip to content

Instantly share code, notes, and snippets.

@bluca bluca/v2_client.cpp
Last active Jul 1, 2017

Embed
What would you like to do?
ZMQ v2 client and v4 server interop
#include <assert.h>
#include <string>
#include <iostream>
#include <zmq.h>
#include <string.h>
int main(int argc, char** argv)
{
unsigned long max_nof_messages = 100;
std::string engine_uri = "tcp://127.0.0.1:5656";
void * context = zmq_init(1);
void * socket = zmq_socket(context, ZMQ_PUSH);
std::cout << "Connecting to " << engine_uri << std::endl;
zmq_connect(socket, engine_uri.c_str());
std::cout << "Start sending..." << std::endl;
unsigned long nof_messages = 0;
while (nof_messages < max_nof_messages) {
zmq_msg_t msg_topic;
int rc = zmq_msg_init_size(&msg_topic, 10);
assert(rc == 0);
memcpy(zmq_msg_data(&msg_topic), "lollollo1", 10);
zmq_msg_t msg_content;
rc = zmq_msg_init_size(&msg_content, 10);
assert(rc == 0);
memcpy(zmq_msg_data(&msg_topic), "lollollo2", 10);
zmq_send(socket, &msg_topic, ZMQ_SNDMORE);
zmq_send(socket, &msg_content, 0);
nof_messages++;
}
zmq_close(socket);
zmq_term(context);
return 0;
}
#include <assert.h>
#include <string>
#include <iostream>
#include <zmq.h>
int main(int argc, char** argv)
{
unsigned long max_nof_messages = 1e6;
std::string engine_uri = "tcp://127.0.0.1:5656";
auto * context = zmq_ctx_new();
auto * socket = zmq_socket(context, ZMQ_PULL);
std::cout << "Binding to " << engine_uri << std::endl;
zmq_bind(socket, engine_uri.c_str());
std::cout << "Start receiving..." << std::endl;
unsigned long nof_messages = 0;
while (nof_messages < max_nof_messages) {
zmq_msg_t msg_topic;
int rc = zmq_msg_init(&msg_topic);
assert(rc == 0);
zmq_msg_t msg_content;
rc = zmq_msg_init(&msg_content);
assert(rc == 0);
zmq_msg_recv(&msg_topic, socket, 0);
zmq_msg_recv(&msg_content, socket, 0);
auto topic_size = zmq_msg_size(&msg_topic);
auto topic_data = zmq_msg_data(&msg_topic);
auto content_size = zmq_msg_size(&msg_content);
auto content_data = zmq_msg_data(&msg_content);
std::string topic(static_cast<char*>(topic_data), topic_size);
std::string content(static_cast<char*>(content_data), content_size);
std::cout << topic << " (size: " << content.size() << ") [Nr." << nof_messages << "]" << std::endl;
nof_messages++;
zmq_msg_close(&msg_topic);
zmq_msg_close(&msg_content);
}
zmq_close(socket);
zmq_term(context);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.