Created
February 27, 2014 18:01
-
-
Save taotetek/9255399 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <zmq.h> | |
#include <czmq.h> | |
#include "include/dbg.h" | |
#define DEBUG 1 | |
int | |
main (void) | |
{ | |
void *ctx = zmq_ctx_new (); | |
void *stream_in = zmq_socket (ctx, ZMQ_STREAM); | |
assert (stream_in); | |
int rc = zmq_bind (stream_in, "tcp://*:9999"); | |
assert (rc == 0); | |
while (1) { | |
/* NOTE - using zmq_recv from libzmq directly, so that I can read | |
* a buffer of a specified length from the socket, since ZMQ_STREAM | |
* is a streaming socket type and does not provide message delimiters | |
*/ | |
char buf[1024]; | |
size_t buf_size = 1024; | |
buf_size = zmq_recv (stream_in, buf, buf_size, 0); | |
/* The first message we receive from the socket should be the zmq id | |
* of the connected client. Print it out to verify */ | |
int i; | |
for (i=0; i<buf_size; i++) { | |
printf ("%02X", (unsigned char) buf [i]); | |
} | |
printf ("\n"); | |
/* The MORE flag should be sent, as each message received from a | |
* ZMQ_STREAM socket should be a two part multi part message - the | |
* first part being the id, the second part being the message itself */ | |
int64_t more; | |
size_t more_size = sizeof (more); | |
zmq_getsockopt (stream_in, ZMQ_RCVMORE, &more, &more_size); | |
/* more SHOULD always be true, to my understanding */ | |
if (more) { | |
buf_size = zmq_recv (stream_in, buf, 1024, 0); | |
for (i=0; i<buf_size; i++) { | |
printf ("%c", buf [i]); | |
} | |
printf ("\n"); | |
} | |
/* if we're here something weird happened or our understanding | |
* of ZMQ_STREAM is wrong. */ | |
else { | |
log_failed ("Something really bad happened - more flag wasn't set."); | |
} | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment