Created
November 18, 2013 02:22
-
-
Save wonkoderverstaendige/7521416 to your computer and use it in GitHub Desktop.
ZeroMQ C++ example server with corresponding Python client. From http://zguide.zeromq.org/page:all
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
# | |
# Hello World client in Python | |
# Connects REQ socket to tcp://localhost:5555 | |
# Sends "Hello" to server, expects "World" back | |
# | |
# INSTALLED VIA EASY_INSTALL | |
import zmq | |
context = zmq.Context() | |
# Socket to talk to server | |
print "Connecting to hello world server..." | |
socket = context.socket(zmq.REQ) | |
socket.connect("tcp://localhost:5555") | |
# Do 10 requests, waiting each time for a response | |
for request in range(10): | |
print "Sending request ", request, "..." | |
socket.send("Hello") | |
# Get the reply. | |
message = socket.recv() | |
print "Received reply ", request, "[", message, "]" |
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
<?xml version="1.0" encoding="utf-8"?> | |
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<ImportGroup Label="PropertySheets" /> | |
<PropertyGroup Label="UserMacros" /> | |
<PropertyGroup> | |
<_PropertySheetDisplayName>ZMQ_DEBUG</_PropertySheetDisplayName> | |
</PropertyGroup> | |
<ItemDefinitionGroup> | |
<ClCompile> | |
<AdditionalIncludeDirectories>%ZMQ_DIR%\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> | |
</ClCompile> | |
<Link> | |
<AdditionalLibraryDirectories>%ZMQ_DIR%\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | |
<AdditionalDependencies>libzmq-v100-mt-3_2_4.lib;libzmq-v100-mt-gd-3_2_4.lib;%(AdditionalDependencies)</AdditionalDependencies> | |
</Link> | |
</ItemDefinitionGroup> | |
<ItemGroup /> | |
</Project> |
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
// | |
// Hello World server in C++ | |
// Binds REP socket to tcp://*:5555 | |
// Expects "Hello" from client, replies with "World" | |
// | |
// TO GET IT TO WORK: | |
// Copy the zm.hpp from https://github.com/zeromq/cppzmq into the include folder of zmq (3.2.4 at the time) | |
// VS needs to specify .lib files of platform! e.g. | |
#include <zmq.hpp> | |
#include <string> | |
#include <iostream> | |
#ifndef _WIN32 | |
#include <unistd.h> | |
#else | |
#include <windows.h> | |
#endif | |
int main () { | |
// Prepare our context and socket | |
zmq::context_t context (1); | |
zmq::socket_t socket (context, ZMQ_REP); | |
socket.bind ("tcp://*:5555"); | |
while (true) { | |
zmq::message_t request; | |
// Wait for next request from client | |
socket.recv (&request); | |
std::cout << "Received Hello" << std::endl; | |
// Do some 'work' | |
#ifndef _WIN32 | |
sleep(1); | |
#else | |
Sleep (1); | |
#endif | |
// 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