1NN_RECV(3) nanomsg 1.1.5 NN_RECV(3)
2
3
4
6 nn_recv - receive a message
7
9 #include <nanomsg/nn.h>
10
11 int nn_recv (int s, void *buf, size_t len, int flags);
12
14 Receive a message from the socket s and store it in the buffer
15 referenced by the buf argument. Any bytes exceeding the length
16 specified by the len argument will be truncated.
17
18 Alternatively, nanomsg can allocate the buffer for you. To do so, let
19 the buf parameter be a pointer to a void* variable (pointer to pointer)
20 to the receive buffer and set the len parameter to NN_MSG. If the call
21 is successful the user is responsible for deallocating the message
22 using the nn_freemsg(3) function.
23
24 The flags argument is a combination of the flags defined below:
25
26 NN_DONTWAIT
27 Specifies that the operation should be performed in non-blocking
28 mode. If the message cannot be received straight away, the function
29 will fail with errno set to EAGAIN.
30
32 If the function succeeds number of bytes in the message is returned.
33 Otherwise, -1 is returned and errno is set to to one of the values
34 defined below.
35
37 EBADF
38 The provided socket is invalid.
39
40 ENOTSUP
41 The operation is not supported by this socket type.
42
43 EFSM
44 The operation cannot be performed on this socket at the moment
45 because socket is not in the appropriate state. This error may
46 occur with socket types that switch between several states.
47
48 EAGAIN
49 Non-blocking mode was requested and there’s no message to receive
50 at the moment.
51
52 EINTR
53 The operation was interrupted by delivery of a signal before the
54 message was received.
55
56 ETIMEDOUT
57 Individual socket types may define their own specific timeouts. If
58 such timeout is hit this error will be returned.
59
60 ETERM
61 The library is terminating.
62
64 Receiving a message into a buffer allocated by the user
65 This example code will retrieve a message of either 100 bytes or
66 less. If a larger message was sent it will be truncated to 100
67 bytes.
68
69 char buf [100];
70 nbytes = nn_recv (s, buf, sizeof (buf), 0);
71
72 Receiving a message into a buffer allocated by nanomsg
73 The following will get a message from the pipe with a buffer
74 allocated by the system. It is large enough to accommodate the
75 entire message. This is a good way to get the entire message
76 without truncating if the size of the message is unknown. It is the
77 user’s responsibility to call nn_freemsg(3) after processing the
78 message.
79
80 void *buf = NULL;
81 nbytes = nn_recv (s, &buf, NN_MSG, 0);
82
83 if (nbytes < 0) {
84 /* handle error */
85 ...
86 }
87 else {
88 /* process message */
89 ...
90 nn_freemsg (buf);
91 }
92
93 Note that this can be more efficient than manually allocating a buffer
94 since it is a zero-copy operation.
95
97 nn_send(3) nn_recvmsg(3) nn_socket(3) nanomsg(7)
98
100 Martin Sustrik <sustrik@250bpm.com>
101
102
103
104 2023-07-20 NN_RECV(3)