Skip to content

Instantly share code, notes, and snippets.

@kuenishi
Created January 31, 2012 10:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save kuenishi/1709739 to your computer and use it in GitHub Desktop.
Save kuenishi/1709739 to your computer and use it in GitHub Desktop.
ZeroMQ sample codes
#include <zmq.hpp>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main (void)
{
zmq::context_t context(1);
const char * protocol =
"tcp://localhost:5555";
// Socket to talk to server
printf ("Connecting to hello world server...");
zmq::socket_t sock (context, ZMQ_SUB);
// sock.bind("epgm://eth0;239.192.1.1:5556");
sock.connect(protocol);
sock.setsockopt (ZMQ_SUBSCRIBE, "", 0);
printf ("done. \n");
int request_nbr;
while(true){
//zmq::message_t request((void*)"Hello", 5, NULL);
// zmq::msg_init_size (&request, 5);
// memcpy (zmq::msg_data (&request), "Hello", 5);
//printf ("Sending Hello %d…\n", request_nbr);
//sock.send ( &request, 0);
//zmq::msg_close (&request);
zmq::message_t reply;
sock.recv (&reply, 0);
printf ("Received Word %d bytes: \"%s\"\n", reply.size(), reply.data());
}
sock.close();
return 0;
}
//
// Hello World server in C++
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <sstream>
#include <unistd.h>
int main () {
const char * protocol =
"tcp://*:5555";
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_PUB);
// socket.setsockopt (ZMQ_RA`TE, &rate, sizeof (rate));
//socket.connect ("epgm://eth0;239.192.1.1:5556");
//socket.bind("epgm://eth0;239.192.1.1:5556");
socket.bind(protocol);
int i = 0;
while (true) {
// Wait for next request from client
// socket.recv (&request);
// Do some 'work'
usleep (100000);
// Send reply back to client
// zmq::message_t reply (5);
// memcpy ((void *) reply.data (), "World", 5);
std::stringstream ss;
ss << "xy.z|Hello: " << i++;
zmq::message_t request((void*)ss.str().c_str(), ss.str().size()+1, NULL);
std::cout << "sending: \"" << (const char*)request.data();
socket.send(request);
std::cout << "\"... done." << std::endl;
}
return 0;
}
#include <zmq.hpp>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main (void)
{
zmq::context_t context(1);
// Socket to talk to server
printf ("Connecting to hello world server…\n");
zmq::socket_t sock (context, ZMQ_REQ);
//sock.connect("tcp://localhost:5555");
sock.connect("ipc:///tmp/test");
int request_nbr;
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
zmq::message_t request((void*)"Hello", 5, NULL);
// zmq::msg_init_size (&request, 5);
// memcpy (zmq::msg_data (&request), "Hello", 5);
printf ("Sending Hello %d…\n", request_nbr);
sock.send ( &request, 0);
//zmq::msg_close (&request);
zmq::message_t reply;
sock.recv ( &reply, 0);
printf ("Received World %d\n", request_nbr);
}
sock.close();
return 0;
}
//
// Hello World server in C++
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <unistd.h>
int main () {
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
//socket.bind ("tcp://*:5555");
socket.bind ("ipc:///tmp/test");
while (true) {
zmq::message_t request;
// Wait for next request from client
socket.recv (&request);
std::cout << "Received Hello" << std::endl;
// Do some 'work'
sleep (1);
// Send reply back to client
zmq::message_t reply (5);
memcpy ((void *) reply.data (), "World", 5);
socket.send (reply);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment