1 .\" Copyright (C) 2011 by Andi Kleen <andi@firstfloor.org>
2 .\" and Copyright (c) 2011 by Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Syscall added in following commit
27 .\" commit a2e2725541fad72416326798c2d7fa4dafb7d337
28 .\" Author: Arnaldo Carvalho de Melo <acme@redhat.com>
29 .\" Date: Mon Oct 12 23:40:10 2009 -0700
31 .TH RECVMMSG 2 2012-12-24 "Linux" "Linux Programmer's Manual"
33 recvmmsg \- receive multiple messages on a socket
36 .B "#define _GNU_SOURCE"
37 .BI "#include <sys/socket.h>"
39 .BI "int recvmmsg(int " sockfd ", struct mmsghdr *" msgvec \
40 ", unsigned int " vlen ","
42 .BI " unsigned int " flags ", struct timespec *" timeout ");"
47 system call is an extension of
49 that allows the caller to receive multiple messages from a socket
50 using a single system call.
51 (This has performance benefits for some applications.)
52 A further extension over
54 is support for a timeout on the receive operation.
58 argument is the file descriptor of the socket to receive data from.
62 argument is a pointer to an array of
65 The size of this array is specified in
70 structure is defined in
77 struct msghdr msg_hdr; /* Message header */
78 unsigned int msg_len; /* Number of received bytes for header */
87 structure, as described in
91 field is the number of bytes returned for the message in the entry.
92 This field has the same value as the return value of a single
98 argument contains flags ORed together.
99 The flags are the same as documented for
101 with the following addition:
103 .BR MSG_WAITFORONE " (since Linux 2.6.34)"
106 after the first message has been received.
113 .BR clock_gettime (2))
114 defining a timeout (seconds plus nanoseconds) for the receive operation.
115 (This interval will be rounded up to the system clock granularity,
116 and kernel scheduling delays mean that the blocking interval
117 may overrun by a small amount.)
120 is NULL, then the operation blocks indefinitely.
126 messages have been received
127 or until the timeout expires.
128 A nonblocking call reads as many messages as are available
129 (up to the limit specified by
131 and returns immediately.
135 successive elements of
137 are updated to contain information about each received message:
139 contains the size of the received message;
142 are updated as described in
144 The return value of the call indicates the number of elements of
146 that have been updated.
150 returns the number of messages received in
152 on error, \-1 is returned, and
154 is set to indicate the error.
158 In addition, the following error can occur:
166 system call was added in Linux 2.6.33.
167 Support in glibc was added in version 2.12.
173 The following program uses
175 to receive multiple messages on a socket and stores
176 them in multiple buffers.
177 The call returns if all buffers are filled or if the
178 timeout specified has expired.
180 The following snippet periodically generates UDP datagrams
181 containing a random number:
185 .RB "$" " while true; do echo $RANDOM > /dev/udp/127.0.0.1/1234; "
186 .B " sleep 0.25; done"
190 These datagrams are read by the example application, which
191 can give the following output:
208 #include <netinet/ip.h>
212 #include <sys/socket.h>
220 int sockfd, retval, i;
221 struct sockaddr_in sa;
222 struct mmsghdr msgs[VLEN];
223 struct iovec iovecs[VLEN];
224 char bufs[VLEN][BUFSIZE+1];
225 struct timespec timeout;
227 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
233 sa.sin_family = AF_INET;
234 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
235 sa.sin_port = htons(1234);
236 if (bind(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == \-1) {
241 memset(msgs, 0, sizeof(msgs));
242 for (i = 0; i < VLEN; i++) {
243 iovecs[i].iov_base = bufs[i];
244 iovecs[i].iov_len = BUFSIZE;
245 msgs[i].msg_hdr.msg_iov = &iovecs[i];
246 msgs[i].msg_hdr.msg_iovlen = 1;
249 timeout.tv_sec = TIMEOUT;
252 retval = recvmmsg(sockfd, msgs, VLEN, 0, &timeout);
254 perror("recvmmsg()");
258 printf("%d messages received\\n", retval);
259 for (i = 0; i < retval; i++) {
260 bufs[i][msgs[i].msg_len] = 0;
261 printf("%d %s", i+1, bufs[i]);
267 .BR clock_gettime (2),
274 This page is part of release 3.65 of the Linux
277 A description of the project,
278 and information about reporting bugs,
280 \%http://www.kernel.org/doc/man\-pages/.