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  re‐
20       ceive 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  re‐
54       ceived  or until the timeout expires.  A nonblocking call reads as many
55       messages as are available (up to the limit specified by vlen)  and  re‐
56       turns 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 ms‐
66       gvec; on error, -1 is returned, and errno is set to indicate the error.
67

ERRORS

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

VERSIONS

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

CONFORMING TO

81       recvmmsg() is Linux-specific.
82

BUGS

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

EXAMPLES

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

SEE ALSO

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

COLOPHON

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