forked from ianbarber/TLSZMQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlsclient.cpp
61 lines (49 loc) · 1.41 KB
/
tlsclient.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
#include "tlszmq.h"
#include <zmq.hpp>
void write_message(TLSZmq *tls, zmq::socket_t *socket) {
if(tls->needs_write()) {
zmq::message_t *data = tls->get_data();
socket->send(*data);
delete data;
}
}
zmq::message_t *read_message(TLSZmq *tls, zmq::socket_t *socket) {
zmq::message_t response;
socket->recv (&response);
tls->put_data(&response);
if(tls->can_recv()) {
return tls->read();
}
return NULL;
}
int main(int argc, char* argv[]) {
try {
SSL_CTX *ssl_context = TLSZmq::init_ctx(TLSZmq::SSL_CLIENT);
zmq::context_t ctx(1);
zmq::socket_t s1(ctx,ZMQ_REQ);
s1.connect ("tcp://localhost:5556");
TLSZmq *tls = new TLSZmq(ssl_context);
bool loop = true;
zmq::message_t request (12);
memcpy(request.data(), "hello world!", 12);
printf("Sending - [%s]\n",(char *)(request.data()));
tls->write(&request);
while (loop) {
write_message(tls, &s1);
zmq::message_t *data = read_message(tls, &s1);
if (NULL != data) {
printf("Received - [%s]\n",(char *)(data->data()));
loop = false;
}
}
// send shutdown to peer
tls->shutdown();
write_message(tls, &s1);
delete tls;
}
catch(std::exception &e) {
printf ("An error occurred: %s\n", e.what());
return 1;
}
return 0;
}