1RECVMMSG(2)                Linux Programmer's Manual               RECVMMSG(2)
2
3
4

NAME

6       recvmmsg - receive multiple messages on a socket
7

SYNOPSIS

9       #define _GNU_SOURCE         /* See feature_test_macros(7) */
10       #include <sys/socket.h>
11
12       int recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
13                    int flags, struct timespec *timeout);
14

DESCRIPTION

16       The  recvmmsg()  system  call is an extension of recvmsg(2) that allows
17       the caller to receive multiple messages from a socket  using  a  single
18       system call.  (This has performance benefits for some applications.)  A
19       further extension over recvmsg(2) is  support  for  a  timeout  on  the
20       receive operation.
21
22       The  sockfd  argument  is  the file descriptor of the socket to receive
23       data from.
24
25       The msgvec argument is a pointer to an  array  of  mmsghdr  structures.
26       The size of this array is specified in vlen.
27
28       The mmsghdr structure is defined in <sys/socket.h> as:
29
30           struct mmsghdr {
31               struct msghdr msg_hdr;  /* Message header */
32               unsigned int  msg_len;  /* Number of received bytes for header */
33           };
34
35       The  msg_hdr  field  is a msghdr structure, as described in recvmsg(2).
36       The msg_len field is the number of bytes returned for  the  message  in
37       the entry.  This field has the same value as the return value of a sin‐
38       gle recvmsg(2) on the header.
39
40       The flags argument contains flags ORed together.   The  flags  are  the
41       same as documented for recvmsg(2), with the following addition:
42
43       MSG_WAITFORONE (since Linux 2.6.34)
44              Turns on MSG_DONTWAIT after the first message has been received.
45
46       The timeout argument points to a struct timespec (see clock_gettime(2))
47       defining a timeout (seconds plus nanoseconds) for the receive operation
48       (but see BUGS!).  (This interval will be rounded up to the system clock
49       granularity, and kernel scheduling delays mean that the blocking inter‐
50       val may overrun by a small amount.)  If timeout is NULL, then the oper‐
51       ation blocks indefinitely.
52
53       A blocking  recvmmsg()  call  blocks  until  vlen  messages  have  been
54       received  or  until  the  timeout expires.  A nonblocking call reads as
55       many messages as are available (up to the limit specified by vlen)  and
56       returns immediately.
57
58       On return from recvmmsg(), successive elements of msgvec are updated to
59       contain information about each received message: msg_len  contains  the
60       size  of  the received message; the subfields of msg_hdr are updated as
61       described in recvmsg(2).  The return value of the  call  indicates  the
62       number of elements of msgvec that have been updated.
63

RETURN VALUE

65       On  success,  recvmmsg()  returns  the  number  of messages received in
66       msgvec; on error, -1 is returned, and errno  is  set  to  indicate  the
67       error.
68

ERRORS

70       Errors  are  as  for  recvmsg(2).  In addition, the following error can
71       occur:
72
73       EINVAL timeout is invalid.
74
75       See also BUGS.
76

VERSIONS

78       The recvmmsg() system call was added in Linux 2.6.33.  Support in glibc
79       was added in version 2.12.
80

CONFORMING TO

82       recvmmsg() is Linux-specific.
83

BUGS

85       The timeout argument does not work as intended.  The timeout is checked
86       only after the receipt of each datagram, so that if up to vlen-1  data‐
87       grams  are  received  before  the  timeout expires, but then no further
88       datagrams are received, the call will block forever.
89
90       If an error occurs after at least one message has  been  received,  the
91       call  succeeds, and returns the number of messages received.  The error
92       code is expected to be returned on a subsequent call to recvmmsg().  In
93       the  current implementation, however, the error code can be overwritten
94       in the meantime by an unrelated network event on a socket, for  example
95       an incoming ICMP packet.
96

EXAMPLE

98       The following program uses recvmmsg() to receive multiple messages on a
99       socket and stores them in multiple buffers.  The call  returns  if  all
100       buffers are filled or if the timeout specified has expired.
101
102       The following snippet periodically generates UDP datagrams containing a
103       random number:
104
105           $ while true; do echo $RANDOM > /dev/udp/127.0.0.1/1234;
106                 sleep 0.25; done
107
108       These datagrams are read by the example application, which can give the
109       following output:
110
111           $ ./a.out
112           5 messages received
113           1 11782
114           2 11345
115           3 304
116           4 13514
117           5 28421
118
119   Program source
120
121       #define _GNU_SOURCE
122       #include <netinet/ip.h>
123       #include <stdio.h>
124       #include <stdlib.h>
125       #include <string.h>
126       #include <sys/socket.h>
127
128       int
129       main(void)
130       {
131       #define VLEN 10
132       #define BUFSIZE 200
133       #define TIMEOUT 1
134           int sockfd, retval, i;
135           struct sockaddr_in addr;
136           struct mmsghdr msgs[VLEN];
137           struct iovec iovecs[VLEN];
138           char bufs[VLEN][BUFSIZE+1];
139           struct timespec timeout;
140
141           sockfd = socket(AF_INET, SOCK_DGRAM, 0);
142           if (sockfd == -1) {
143               perror("socket()");
144               exit(EXIT_FAILURE);
145           }
146
147           addr.sin_family = AF_INET;
148           addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
149           addr.sin_port = htons(1234);
150           if (bind(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
151               perror("bind()");
152               exit(EXIT_FAILURE);
153           }
154
155           memset(msgs, 0, sizeof(msgs));
156           for (i = 0; i < VLEN; i++) {
157               iovecs[i].iov_base         = bufs[i];
158               iovecs[i].iov_len          = BUFSIZE;
159               msgs[i].msg_hdr.msg_iov    = &iovecs[i];
160               msgs[i].msg_hdr.msg_iovlen = 1;
161           }
162
163           timeout.tv_sec = TIMEOUT;
164           timeout.tv_nsec = 0;
165
166           retval = recvmmsg(sockfd, msgs, VLEN, 0, &timeout);
167           if (retval == -1) {
168               perror("recvmmsg()");
169               exit(EXIT_FAILURE);
170           }
171
172           printf("%d messages received\n", retval);
173           for (i = 0; i < retval; i++) {
174               bufs[i][msgs[i].msg_len] = 0;
175               printf("%d %s", i+1, bufs[i]);
176           }
177           exit(EXIT_SUCCESS);
178       }
179

SEE ALSO

181       clock_gettime(2),   recvmsg(2),   sendmmsg(2),  sendmsg(2),  socket(2),
182       socket(7)
183

COLOPHON

185       This page is part of release 5.02 of the Linux  man-pages  project.   A
186       description  of  the project, information about reporting bugs, and the
187       latest    version    of    this    page,    can     be     found     at
188       https://www.kernel.org/doc/man-pages/.
189
190
191
192Linux                             2019-03-06                       RECVMMSG(2)
Impressum