1 .\" Copyright (C) 2011 by Andi Kleen <andi@firstfloor.org>
2 .\" and Copyright (c) 2011 by Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" Syscall added in following commit
7 .\" commit a2e2725541fad72416326798c2d7fa4dafb7d337
8 .\" Author: Arnaldo Carvalho de Melo <acme@redhat.com>
9 .\" Date: Mon Oct 12 23:40:10 2009 -0700
11 .TH RECVMMSG 2 2020-11-01 "Linux" "Linux Programmer's Manual"
13 recvmmsg \- receive multiple messages on a socket
16 .RI ( libc ", " \-lc )
19 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
20 .B #include <sys/socket.h>
22 .BI "int recvmmsg(int " sockfd ", struct mmsghdr *" msgvec \
23 ", unsigned int " vlen ","
24 .BI " int " flags ", struct timespec *" timeout ");"
29 system call is an extension of
31 that allows the caller to receive multiple messages from a socket
32 using a single system call.
33 (This has performance benefits for some applications.)
34 A further extension over
36 is support for a timeout on the receive operation.
40 argument is the file descriptor of the socket to receive data from.
44 argument is a pointer to an array of
47 The size of this array is specified in
52 structure is defined in
59 struct msghdr msg_hdr; /* Message header */
60 unsigned int msg_len; /* Number of received bytes for header */
69 structure, as described in
73 field is the number of bytes returned for the message in the entry.
74 This field has the same value as the return value of a single
80 argument contains flags ORed together.
81 The flags are the same as documented for
83 with the following addition:
85 .BR MSG_WAITFORONE " (since Linux 2.6.34)"
88 after the first message has been received.
95 .BR clock_gettime (2))
96 defining a timeout (seconds plus nanoseconds) for the receive operation
97 .RI ( "but see BUGS!" ).
98 (This interval will be rounded up to the system clock granularity,
99 and kernel scheduling delays mean that the blocking interval
100 may overrun by a small amount.)
103 is NULL, then the operation blocks indefinitely.
109 messages have been received
110 or until the timeout expires.
111 A nonblocking call reads as many messages as are available
112 (up to the limit specified by
114 and returns immediately.
118 successive elements of
120 are updated to contain information about each received message:
122 contains the size of the received message;
125 are updated as described in
127 The return value of the call indicates the number of elements of
129 that have been updated.
133 returns the number of messages received in
135 on error, \-1 is returned, and
137 is set to indicate the error.
141 In addition, the following error can occur:
151 system call was added in Linux 2.6.33.
152 Support in glibc was added in version 2.12.
159 argument does not work as intended.
160 .\" FIXME . https://bugzilla.kernel.org/show_bug.cgi?id=75371
161 .\" http://thread.gmane.org/gmane.linux.man/5677
162 The timeout is checked only after the receipt of each datagram,
165 datagrams are received before the timeout expires,
166 but then no further datagrams are received, the call will block forever.
168 If an error occurs after at least one message has been received,
169 the call succeeds, and returns the number of messages received.
170 The error code is expected to be returned on a subsequent call to
172 In the current implementation, however, the error code can be overwritten
173 in the meantime by an unrelated network event on a socket,
174 for example an incoming ICMP packet.
176 The following program uses
178 to receive multiple messages on a socket and stores
179 them in multiple buffers.
180 The call returns if all buffers are filled or if the
181 timeout specified has expired.
183 The following snippet periodically generates UDP datagrams
184 containing a random number:
188 .RB "$" " while true; do echo $RANDOM > /dev/udp/127.0.0.1/1234;"
189 .B " sleep 0.25; done"
193 These datagrams are read by the example application, which
194 can give the following output:
211 #include <netinet/ip.h>
215 #include <sys/socket.h>
224 struct sockaddr_in addr;
225 struct mmsghdr msgs[VLEN];
226 struct iovec iovecs[VLEN];
227 char bufs[VLEN][BUFSIZE+1];
228 struct timespec timeout;
230 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
236 addr.sin_family = AF_INET;
237 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
238 addr.sin_port = htons(1234);
239 if (bind(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == \-1) {
244 memset(msgs, 0, sizeof(msgs));
245 for (int i = 0; i < VLEN; i++) {
246 iovecs[i].iov_base = bufs[i];
247 iovecs[i].iov_len = BUFSIZE;
248 msgs[i].msg_hdr.msg_iov = &iovecs[i];
249 msgs[i].msg_hdr.msg_iovlen = 1;
252 timeout.tv_sec = TIMEOUT;
255 retval = recvmmsg(sockfd, msgs, VLEN, 0, &timeout);
257 perror("recvmmsg()");
261 printf("%d messages received\en", retval);
262 for (int i = 0; i < retval; i++) {
263 bufs[i][msgs[i].msg_len] = 0;
264 printf("%d %s", i+1, bufs[i]);
270 .BR clock_gettime (2),