/
client.c
68 lines (53 loc) · 1.27 KB
/
client.c
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
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <zmq.h>
#define BUFFER_LENGTH 32
int main()
{
char buffer[BUFFER_LENGTH];
char *address = "tcp://localhost:5556";
int rc;
void *context = zmq_ctx_new();
if (context == NULL) {
perror("zmq_ctx_new() failed");
return -1;
}
void *socket = zmq_socket(context, ZMQ_PAIR);
if (socket == NULL) {
perror("zmq_socket() failed");
zmq_ctx_destroy(context);
return -1;
}
rc = zmq_connect(socket, address);
if (rc != 0) {
perror("cannot connect()");
zmq_close(socket);
zmq_ctx_destroy(context);
return -1;
}
printf("Connected to address %s\n", address);
while (1)
{
int num = zmq_recv(socket, buffer, BUFFER_LENGTH-1, 0);
if (num < 0) {
perror("zmq_recv() failed");
}
else {
buffer[num] = '\0';
printf("Received '%s'\n", buffer);
}
}
rc = zmq_close(socket);
if (rc != 0) {
perror("cannot close socket");
zmq_ctx_destroy(context);
return -1;
}
rc = zmq_ctx_destroy(context);
if (rc != 0) {
perror("zmq_ctx_destroy() failed");
return -1;
}
return 0;
}