Created
January 3, 2011 21:25
-
-
Save thomasluce/763990 to your computer and use it in GitHub Desktop.
a simple publisher and subscriber using zeromq
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
########################### pub.c ########################################## | |
#include "zmq.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(int argc, char **argv) { | |
void *context = zmq_init(1); | |
void *socket = zmq_socket(context, ZMQ_PUB); | |
zmq_connect(socket, "tcp://127.0.0.1:12345"); | |
sleep(1); | |
char message[1000] = "testing\n"; | |
while(1) { | |
zmq_msg_t out_msg; | |
zmq_msg_init_size(&out_msg, strlen(message)); | |
memcpy(zmq_msg_data(&out_msg), message, strlen(message)); | |
zmq_send(socket, &out_msg, 0); | |
zmq_msg_close(&out_msg); | |
sleep(1); | |
} | |
} | |
########################### sub.c ########################################## | |
#include "zmq.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(int argc, char **argv) { | |
void *context = zmq_init(1); | |
void *socket = zmq_socket(context, ZMQ_SUB); | |
zmq_connect(socket, "tcp://127.0.0.1:12345"); | |
zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "", 0); | |
char string[1000] = ""; | |
while(1) { | |
zmq_msg_t in_msg; | |
zmq_msg_init(&in_msg); | |
zmq_recv(socket, &in_msg, 0); | |
int size = zmq_msg_size (&in_msg); | |
memcpy(string, zmq_msg_data(&in_msg), size); | |
zmq_msg_close(&in_msg); | |
string[size] = 0; | |
printf("%s\n", string); | |
sleep(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment