Skip to content

Instantly share code, notes, and snippets.

@lleqsnoom
Created August 23, 2017 13:24
Show Gist options
  • Save lleqsnoom/62678374b0e9a2a747689be8e8c20cac to your computer and use it in GitHub Desktop.
Save lleqsnoom/62678374b0e9a2a747689be8e8c20cac to your computer and use it in GitHub Desktop.
//
// Hello World server in C++
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include "zmq.hpp"
#include <string>
#include <sstream>
#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);
zmq::socket_t socket (context, ZMQ_ROUTER);
socket.bind ("tcp://*:5555");
std::string out = "";
printf("%s\n", "__Start__");
while (true) {
zmq::message_t request;
socket.recv (&request);
std::string *sp = static_cast<std::string*>(request.data());
std::string str = *sp;
delete sp;
// printf("____ \n%s\n____\n", request.data());
out.append(str);
std::size_t found = out.rfind("\r\n\r\n");
printf("____ \n%s\n____", out.c_str());
std::cout << found << " " << std::string::npos << '\n';
if( found != std::string::npos ){
printf("%s\n", out.c_str());
// Send reply back to client
zmq::message_t reply (78);
memcpy (reply.data (),
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 13\r\n"
"\r\n"
"Hello, World!"
, 78);
printf("%s\n", "__Send__");
printf("%s\n", reply.data ());
socket.send (reply);
out = "";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment