Skip to content

Instantly share code, notes, and snippets.

@OlegJakushkin
Created April 29, 2017 03:29
Show Gist options
  • Save OlegJakushkin/9a62d333028a93264ec4517c5429658c to your computer and use it in GitHub Desktop.
Save OlegJakushkin/9a62d333028a93264ec4517c5429658c to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <pico.h>
#include <zmq_addon.hpp>
#include <atomic>
#include <thread>
using namespace std;
using namespace zmq;
atomic<bool> stop;
int main() {
context_t ctx;
auto server = [&]() {
socket_t socket(ctx, ZMQ_REP);
socket.bind("tcp://*:5555");
while(!stop) {
message_t identity;
message_t msg;
socket.recv(&identity);
string requestType(identity.data<char>(), identity.size());
socket.recv(&msg);
string request(msg.data<char>(), msg.size());
cout << request << endl;
string reply = "hello";
message_t rep(reply.data(), reply.size());
socket.send(rep);
}
};
auto client = [&]() {
socket_t socket(ctx, ZMQ_REQ);
socket.connect("tcp://127.0.0.1:5555");
while(!socket.connected()) {
this_thread::sleep_for(chrono::milliseconds(100));
}
while(!stop) {
string requestType = "simple";
string requestContent = "some complex json string";
message_t identity(requestType.data(), requestType.size());
message_t msg(requestContent.data(), requestContent.size());
socket.send(identity, ZMQ_SNDMORE);
socket.send(msg);
cout << "sent ok" << endl;
message_t rep;
socket.recv(&rep);
cout << "received: " << string(rep.data<char>(), rep.size()) << endl;
}
};
thread s(server);
thread c(client);
while(!stop) {
string exit;
cin >> exit;
if(exit == "exit") {
stop = true;
} else {
cout << "enter 'exit' to quit" << endl;
}
}
s.join();
c.join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment