Created
April 28, 2013 00:51
-
-
Save chiral/5475355 to your computer and use it in GitHub Desktop.
PUB/SUB for respective process on ZeroMQ example.
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 <stdio.h> | |
| #include <stdlib.h> | |
| #include <time.h> | |
| #include <assert.h> | |
| #include <unistd.h> | |
| #include <zmq.hpp> | |
| const int max_peer = 3; | |
| int main() { | |
| zmq::context_t context(1); | |
| zmq::socket_t sock(context,ZMQ_PUB); | |
| sock.bind("ipc:///test1"); | |
| while (1) { | |
| sleep(1); | |
| char buf[256]; | |
| int len; | |
| int peer = rand()%max_peer; | |
| len = sprintf(buf,"%d ",peer); | |
| sock.send(buf,len,ZMQ_SNDMORE); | |
| len = sprintf(buf,"%d ",rand()); | |
| sock.send(buf,len,0); | |
| } | |
| } |
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 <stdio.h> | |
| #include <stdlib.h> | |
| #include <time.h> | |
| #include <assert.h> | |
| #include <unistd.h> | |
| #include <zmq.hpp> | |
| int main(int argc, char *argv[]) { | |
| if (argc<2) return 0; | |
| int peer_id = atoi(argv[1]); | |
| printf("peer_id=%d\n",peer_id); | |
| zmq::context_t context(1); | |
| zmq::socket_t sock(context,ZMQ_SUB); | |
| sock.connect("ipc:///test1"); | |
| char buf[256]; | |
| int len = sprintf(buf,"%d ",peer_id); | |
| sock.setsockopt(ZMQ_SUBSCRIBE,buf,len); | |
| while (1) { | |
| zmq::message_t msg; | |
| sock.recv(&msg); | |
| memcpy(buf,msg.data(),msg.size()); | |
| buf[msg.size()]=0; | |
| puts(buf); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment