pBook‎ > ‎

ZMQ

Example client server

posted May 29, 2016, 6:07 AM by Javad Taghia   [ updated May 29, 2016, 6:09 AM ]

#SERVER
//  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>
#ifndef _WIN32
#include <unistd.h>
#else
#include <windows.h>

#define sleep(n)    Sleep(n)
#endif

int main () {
    //  Prepare our context and socket
    zmq::context_t context (1);
    zmq::socket_t socket (context, ZMQ_REP);
    socket.bind ("tcp://*:9998");

    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 (reply.data (), "World", 5);
        socket.send (reply);
    }
    return 0;
}

#CLIENT
#include <zmq.hpp>
#include <string>
#include <iostream>
#ifndef _WIN32
#include <unistd.h>
#else
#include <windows.h>

#define sleep(n)    Sleep(n)
#endif

int main ()
{
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REQ);

socket.connect("tcp://127.0.0.1:9998");

while (true)
{

  //  Do some 'work'
        sleep(10);

      zmq::message_t reply (5);
        memcpy (reply.data (), "World", 5);
socket.send( reply);
zmq::message_t request;
  //  Wait for next request from client
        socket.recv (&request);
        std::cout << "Received Hello" << std::endl;
}
return 0;
}

Getting Started with the ZeroMQ (ØMQ) C++ Bindings

posted May 29, 2016, 6:06 AM by Javad Taghia   [ updated May 29, 2016, 6:06 AM ]

Getting Started with the ZeroMQ (ØMQ) C++ Bindings

February 28, 2015

Why ØMQ?

At a previous job some friends were working on a team creating a satellite communication system for cube-sat swarms. They started with the Node.js platform running on all the satellites, and for their communication protocol they decided on ZeroMQ.

Recently I became curious about running a service that would use High Performance Computing to solve a geopotential calculation. At first I thought it might be interesting to create a native add-on for Node.js that would provid access to the GPGPU processing power of one of Amazon's compute instances. But writing a native add-on isn't a trivial task and it seems more valuable to learn ZeroMQ and have node communicate directly with a C++ ZeroMQ enabled server.

This post is only about the process of getting the ZeroMQ hello world application up and running.

Installing ZeroMQ on a Macbook

I'm going to use Apple's XCode (you must install XCode before proceeding further through this tutorial) to write my C++ code in, so I need to setup a project that references the zeromqlib. This requires installing a bunch of build tools. I'll be using homebrew for my instalation supporting libraries. The requirements for building from source are listed on the ZeroMQ doc page. Depending on your setup a lot of these libraries are already installed.

zeromq-4.1.0-rc1.tar.gz is the POSIX tarball I downloaded from http://zeromq.org/area:download.

$ brew update
$ brew install libtool --universal
$ brew update libtool
$ brew install pkg-config --universal
$ brew update pkg-config
$ brew install autoconf --universal
$ brew update autoconf
$ brew install automake --universal
$ brew update automake
$ tar -xvzf zeromq-4.1.0-rc1.tar.gz
$ cd zeromq-4.1.0
$ ./configure
$ make
$ make install

Side note: build-essential is specific to Ubuntu therefore and is therefore excluded from the above bash commands for my mac install.

You can do all the above installing and making from scratch OR you can run the infinitely easier brew install call:

$ brew install --universal zeromq

ZeroMQ C++ Bindings

Next you'll need to download the C++ binding file from github. Then you'll copy the binding header file zmq.hpp into the include directory for the zeromq library you just built. In my case I copied it into /usr/local/include. That was easy, right?

XCode Project Creation and Setup

Now we need to create the client and server "Hello World" XCode projects from the ZeroMQ tutorial. In XCode, create a new command line application project for both the client "Hello World" application and the server "Hello World" application.

create xcode command line project

Once you click next you'll name the projects hello-world-client and hello-world-server and from the languages dropdown you'll select C++. For the client application you'll delete the main.cpp file and copy in the hwclient.cpp from the zguide github. And for the server application you'll delete the main.cpp file and copy in the hwserver.cpp from the zguide github.

If you build now you will get a build error "Lexical or Preprocessor Issue 'zmq.hpp' file not found". In order to get rid of this we need to adjust our Header Search Paths to include the location of our zeromq C++ binding header file and our other zeromq library headers. Add /usr/local/include to the Header Search Paths in the Search Pathssection of the Build Settings
header search paths

Now if we build we'll probably get a lot of "Apple Mach-O Linker(id) Error" errors. Add -lzmq to the Other Linker Flagslist in the Linking section of the Build Settings
lzmq other linker flags

Now if we build we'll have one last "Apple Mach-O Linker(id) Error" error. This is because we haven't indicated the location of our zeromq libraries. Add /usr/local/lib to the Library Search Paths in the Search Paths section of theBuild Settings
Library Search Paths

Our build should be successful! After you've updated the Header Search Paths, the Other Linker Flags, and the Library Search Paths for both projects we should be building without any problems. Now if we run both projects we should have successful communication between the client and server!!!!

Successful communications!!

Setup visual studio 2012,15

posted May 29, 2016, 6:05 AM by Javad Taghia   [ updated May 29, 2016, 6:05 AM ]

SETTING UP ZEROMQ C++ ON VISUAL STUDIO

This article is tested against following version / configuration:

  • Visual Studio 2012 (V110) Platform Toolset
  • Win32
  • ZeroMQ 4.0.4
  • Runtime Library: Multi-threaded DLL (/MD)

Steps to setup ZeroMQ on a Visual Studio C++ project:

  1. Download and install ZeroMQ-4.0.4~miru1.0-x86.exe (or newer) from http://zeromq.org/distro:microsoft-windows
  2. Set ZEROMQ_HOME environment variable to C:\Program Files (x86)\ZeroMQ 4.0.4 (or wherever you installed it to)
  3. On Visual Studio project configuration, add $(ZEROMQ_HOME)\include to Configuration Properties -> VC++ Directories. Don’t forget to restart Visual Studio so it picks up the new environment variable
  4. Add $(ZEROMQ_HOME)\lib to Linker -> General -> Additional Library Directories
  5. Add libzmq-v110-mt-4_0_4.lib to Linker -> Input -> Additional Dependencies
  6. Get a copy of zmq.hpp from https://github.com/zeromq/cppzmq, place this somewhere on your project. This header file references zmq.h located at $(ZEROMQ_HOME)\include
  7. Once you’ve compiled your executable, place libzmq-v110-mt-4_0_4.dll on the same folder, otherwise ZeroMQ initialisation will cause runtime crash

And finally take a look at some excellent tutorial examples from zeromq.org website to get you started, in particular:

See Also

Download ZMQ

posted May 29, 2016, 6:03 AM by Javad Taghia   [ updated May 29, 2016, 6:03 AM ]

http://zeromq.org/distro:microsoft-windows

Stable Release 4.0.4

Binaries built with v90, v100, v110, v110_xp, and v120 toolkits. Windows XP 64-bit is not supported for x64 binaries. Library naming scheme follows Boost library naming for convenience. Packages include release and debug dynamic libraries along with PDB files for the debug builds, static libraries are set to appear in the next stable revision.

The following x86 libraries will work under Windows XP SP3:

CompilerToolkitReleaseDebug
Visual Studio 2008 SP1v90libzmq-v90-mt-4_0_4libzmq-v90-mt-gd-4_0_4
Visual Studio 2010 SP1v100libzmq-v100-mt-4_0_4libzmq-v100-mt-gd-4_0_4
Visual Studio 2012 U4v110_xplibzmq-v110_xp-mt-4_0_4libzmq-v110_xp-mt-gd-4_0_4

1-4 of 4