-
Notifications
You must be signed in to change notification settings - Fork 4
/
first.c
72 lines (59 loc) · 1.57 KB
/
first.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
68
69
70
71
72
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <nanomsg/nn.h>
#include <nanomsg/pipeline.h>
const char *URL = "ipc:///tmp/example5";
void report_error(const char *func)
{
fprintf(stderr, "%s: %s\n", func, nn_strerror(nn_errno()));
exit(1);
}
void send_message(const int socket, const char *message)
{
int bytes;
printf("Sending message '%s'\n", message);
if ((bytes = nn_send(socket, message, strlen(message)+1, 0)) < 0) {
report_error("nn_send");
}
printf("Message with length %d bytes sent, flushing\n", bytes);
sleep(1);
}
void receive_response(const int socket)
{
char *response = NULL;
int bytes;
if ((bytes = nn_recv(socket, &response, NN_MSG, 0)) < 0) {
report_error("nn_recv");
}
printf("Received response '%s' with length %d bytes\n", response, bytes);
if (nn_freemsg(response) < 0) {
report_error("nn_freemsg");
}
}
void first(const char *url)
{
int socket;
int endpoint;
if ((socket = nn_socket(AF_SP, NN_PULL)) < 0) {
report_error("nn_socket");
}
puts("Socket created");
if ((endpoint = nn_connect(socket, url)) < 0) {
report_error("nn_connect");
}
puts("Remote endpoint added to the socket");
send_message(socket, "Hello from 'first'!");
puts("Waiting for response...");
receive_response(socket);
if (nn_shutdown(socket, endpoint) < 0) {
report_error("nn_shutdown");
}
puts("Shutdown completed");
}
int main(const int argc, const char **argv)
{
first(URL);
return 0;
}