-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-dealer-router-async-client.cpp
63 lines (55 loc) · 1.73 KB
/
10-dealer-router-async-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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <vector>
#include <thread>
#include <memory>
#include <functional>
#include <string>
#include <zmq.hpp>
#include <iostream>
#include <unistd.h>
class client_task {
public:
client_task(std::string ident)
: ctx_(1),
client_socket_(ctx_, ZMQ_DEALER),
ident_(ident)
{}
void start() {
char identity[10] = {};
// zmq_identity option
client_socket_.setsockopt(ZMQ_IDENTITY, ident_.c_str(), ident_.length());
client_socket_.connect("tcp://localhost:5570");
std::cout << "Client " << ident_ << " started" <<std::endl;
zmq::pollitem_t items[] = {
{ client_socket_, 0, ZMQ_POLLIN, 0 } };
int request_nbr = 0;
try {
while (true) {
request_nbr += 1;
std::cout << "Req #" << request_nbr << std::endl;
char request_string[16] = {};
sprintf(request_string, "request #%d", request_nbr);
client_socket_.send(request_string, strlen(request_string));
sleep(1);
zmq::poll(items, 1, 1000);
if (items[0].revents & ZMQ_POLLIN) {
zmq::message_t msg;
client_socket_.recv(msg);
std::string smsg(static_cast<char*>(msg.data()), msg.size());
std::cout << ident_ << " received: " << smsg << std::endl;
}
}
}
catch (std::exception &e) {}
}
private:
zmq::context_t ctx_;
zmq::socket_t client_socket_;
std::string ident_;
};
int main(int argc, char* argv[]){
client_task ct(argv[1]);
std::thread t1(std::bind(&client_task::start, &ct));
t1.detach();
getchar();
return 0;
}