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
64
65
66
67
68
69
70
71
| #include <stdlib.h>
| #include <stdio.h>
| #include <string.h>
| #include <time.h>
|
| #include <nng/nng.h>
| #include <nng/protocol/reqrep0/rep.h>
| #include <nng/protocol/reqrep0/req.h>
|
| #define NODE0 "node0"
| #define NODE1 "node1"
| #define DATE "DATE"
|
| void
| fatal(const char *func, int rv)
| {
| fprintf(stderr, "%s: %s\n", func, nng_strerror(rv));
| exit(1);
| }
|
| char *
| date(void)
| {
| time_t now = time(&now);
| struct tm *info = localtime(&now);
| char *text = asctime(info);
| text[strlen(text)-1] = '\0'; // remove '\n'
| return (text);
| }
|
|
| int
| client(const char *url)
| {
| nng_socket sock;
| int rv;
| size_t sz;
| char * buf;
| char sendbuf[512];
|
| if ((rv = nng_req0_open(&sock)) != 0) {
| fatal("nng_socket", rv);
| }
| if ((rv = nng_dial(sock, url, NULL, 0)) != 0) {
| fatal("nng_dial", rv);
| }
| //printf("NODE1: SENDING DATE REQUEST %s\n", DATE);
| while(true) {
| printf("say something:\n");
| scanf("%s", sendbuf);
| if ((rv = nng_send(sock, sendbuf, strlen(sendbuf)+1, 0)) != 0) {
| fatal("nng_send", rv);
| }
| if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
| fatal("nng_recv", rv);
| }
| printf("NODE1: RECEIVED DATE %s\n", buf);
| nng_free(buf, sz);
| }
|
| nng_close(sock);
| return (0);
| }
|
| int
| main(const int argc, const char **argv)
| {
|
| char *url = "ipc:///tmp/reqrep.ipc";
| client(url);
| }
|
|