Skip to content

Instantly share code, notes, and snippets.

@mivade
Last active April 12, 2017 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mivade/09185f794df17785c99655adcc965e32 to your computer and use it in GitHub Desktop.
Save mivade/09185f794df17785c99655adcc965e32 to your computer and use it in GitHub Desktop.
Sending raw bytes over ZMQ sockets with C++

Demo of sending raw bytes over ZMQ sockets with C++.

This assumes you are using miniconda and have installed cppzmq:

conda install -c conda-forge cppzmq
cmake_minimum_required(VERSION 3.6)
project(zmqcpp)
find_library(LIBZMQ libzmq.a PATHS ~/miniconda3/lib)
include_directories(~/miniconda3/include)
add_executable(${CMAKE_PROJECT_NAME} main.cpp)
set_property(TARGET ${CMAKE_PROJECT_NAME} PROPERTY CXX_STANDARD 14)
target_link_libraries(${CMAKE_PROJECT_NAME} ${LIBZMQ})
#include <iostream>
#include <sstream>
#include <thread>
#include <chrono>
#include <zmq.hpp>
int main() {
zmq::context_t ctx(1);
zmq::socket_t socket(ctx, ZMQ_PUB);
socket.bind("tcp://*:9090");
for (double i = 0; i < 1000; i++) {
std::ostringstream stream;
stream.write(reinterpret_cast<char *>(&i), sizeof(i));
std::cout << "Sending " << i << std::endl;;
zmq::message_t msg(stream.str().c_str(), stream.str().length());
socket.send(msg);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment