-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-pub-sub-and-pull-push-client.cpp
49 lines (39 loc) · 1.33 KB
/
06-pub-sub-and-pull-push-client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <zmq.hpp>
#include <iostream>
#include <sstream>
#include <unistd.h>
#include <cstdlib>
#include <ctime>
int main ()
{
srand((unsigned int)time(NULL));
zmq::context_t context (1);
zmq::socket_t subscriber (context, zmq::socket_type::sub);
subscriber.connect("tcp://localhost:5557");
subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);
zmq::socket_t publisher(context, ZMQ_PUSH);
publisher.connect("tcp://localhost:5558");
while (1){
zmq_pollitem_t items[1];
items[0].socket = subscriber;
items[0].events = ZMQ_POLLIN;
zmq::poll(items, 1, 100);
// msg 왔는지 확인
if (items[0].revents & ZMQ_POLLIN){
zmq::message_t message(4);
subscriber.recv(message, zmq::recv_flags::none);
std::string smessage(static_cast<char*>(message.data()), message.size());
std::cout << "I: received message " << smessage << std::endl;
}
else{
zmq::message_t message(4);
int rand = std::rand() % 101;
if (rand < 10){
snprintf((char *) message.data(), 4,"%d", rand);
publisher.send(message, zmq::send_flags::none);
std::cout << "I: sending message " << rand << std::endl;
}
}
}
return 0;
}