Showing the changes to src/cppzmq-test.cpp (1 of 6 files). Show all
Unified · Split
| @@ -0,0 +1,44 @@ | |||
| 1 | /* | ||
| 2 | * This file is part of MXE. See LICENSE.md for licensing information. | ||
| 3 | */ | ||
| 4 | |||
| 5 | |||
| 6 | // Taken from: http://zguide.zeromq.org/cpp:hwserver | ||
| 7 | // Hello World server in C++ | ||
| 8 | // Binds REP socket to tcp://*:5555 | ||
| 9 | // Expects "Hello" from client, replies with "World" | ||
| 10 | // | ||
| 11 | #include <zmq.hpp> | ||
| 12 | #include <string> | ||
| 13 | #include <iostream> | ||
| 14 | #ifndef _WIN32 | ||
| 15 | #include <unistd.h> | ||
| 16 | #else | ||
| 17 | #include <windows.h> | ||
| 18 | |||
| 19 | #define sleep(n) Sleep(n) | ||
| 20 | #endif | ||
| 21 | |||
| 22 | int main () { | ||
| 23 | // Prepare our context and socket | ||
| 24 | zmq::context_t context (1); | ||
| 25 | zmq::socket_t socket (context, ZMQ_REP); | ||
| 26 | socket.bind ("tcp://*:5555"); | ||
| 27 | |||
| 28 | while (true) { | ||
| 29 | zmq::message_t request; | ||
| 30 | |||
| 31 | // Wait for next request from client | ||
| 32 | socket.recv (&request); | ||
| 33 | std::cout << "Received Hello" << std::endl; | ||
| 34 | |||
| 35 | // Do some 'work' | ||
| 36 | sleep(1); | ||
| 37 | |||
| 38 | // Send reply back to client | ||
| 39 | zmq::message_t reply (5); | ||
| 40 | memcpy (reply.data (), "World", 5); | ||
| 41 | socket.send (reply); | ||
| 42 | } | ||
| 43 | return 0; | ||
| 44 | } | ||