Created
May 18, 2015 17:36
-
-
Save whoshuu/09b7e4c98938c043706f to your computer and use it in GitHub Desktop.
Sending a protobuf message using zmq
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> | |
#include "basic.pb.h" | |
int main(int argc, char** argv) { | |
zmq::context_t context{1}; | |
zmq::socket_t sender{context, ZMQ_PAIR}; | |
zmq::socket_t receiver{context, ZMQ_PAIR}; | |
sender.bind("tcp://*:*"); | |
char raw_address[1024]; | |
size_t size = sizeof(raw_address); | |
sender.getsockopt(ZMQ_LAST_ENDPOINT, &raw_address, &size); | |
auto address = std::string{raw_address}; | |
auto port = std::stoi(address.substr(address.find(':', 4) + 1, address.length())); | |
std::stringstream url; | |
url << "tcp://0.0.0.0:" << port; | |
receiver.connect(url.str().data()); | |
Basic basic; | |
basic.set_value("hello world!"); | |
std::ostringstream output_stream; | |
basic.SerializeToOstream(&output_stream); | |
auto output_string = output_stream.str(); | |
zmq::message_t output{output_string.length()}; | |
memcpy(output.data(), output_string.data(), output_string.length()); | |
sender.send(message); | |
zmq::message_t input; | |
Basic received; | |
receiver.recv(&input); | |
received.ParseFromString(std::string{(char*) input.data(), input.size()}); | |
std::cout << received.value() << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment