Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hello World HTTP server using ZeroMQ * Build and install libzmq and CZMQ * Full article on http://hintjens.com/blog:42
// Minimal HTTP server in 0MQ
#include "czmq.h"
int main (void)
{
zctx_t *ctx = zctx_new ();
void *router = zsocket_new (ctx, ZMQ_ROUTER);
zsocket_set_router_raw (router, 1);
int rc = zsocket_bind (router, "tcp://*:8080");
assert (rc != -1);
while (true) {
// Get HTTP request
zframe_t *handle = zframe_recv (router);
if (!handle)
break; // Ctrl-C interrupt
char *request = zstr_recv (router);
puts (request); // Professional Logging(TM)
free (request); // We throw this away
// Send Hello World response
zframe_send (&handle, router, ZFRAME_MORE + ZFRAME_REUSE);
zstr_send (router, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nHello, World!");
// Close connection to browser
zframe_send (&handle, router, ZFRAME_MORE);
zmq_send (router, NULL, 0, 0);
}
zctx_destroy (&ctx);
return 0;
}
@vortechs2000

This comment has been minimized.

Show comment
Hide comment
@vortechs2000

vortechs2000 Apr 29, 2013

I'm not getting the connection to my browser closed when using this with zeromq 3.2.2 and relatively current czmq from github - am I doing something wrong?

vortechs2000 commented Apr 29, 2013

I'm not getting the connection to my browser closed when using this with zeromq 3.2.2 and relatively current czmq from github - am I doing something wrong?

@raffian

This comment has been minimized.

Show comment
Hide comment
@raffian

raffian May 16, 2013

Is there a Java version for this?

raffian commented May 16, 2013

Is there a Java version for this?

@malexer

This comment has been minimized.

Show comment
Hide comment
@malexer

malexer commented Jan 28, 2014

Here is a Python version: http://gist.github.com/malexer/8664997

@githwxi

This comment has been minimized.

Show comment
Hide comment
@githwxi

githwxi Nov 28, 2014

By the way, your code may have forgotten to close the router socket at the end.

githwxi commented Nov 28, 2014

By the way, your code may have forgotten to close the router socket at the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment