1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" with some material from a draft by
3 .\" Stephan Mueller <stephan.mueller@atsec.com>
4 .\" in turn based on Andi Kleen's recvmmsg.2 page.
6 .\" %%%LICENSE_START(VERBATIM)
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
28 .TH SENDMMSG 2 2018-02-02 "Linux" "Linux Programmer's Manual"
30 sendmmsg \- send multiple messages on a socket
33 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
34 .BI "#include <sys/socket.h>"
36 .BI "int sendmmsg(int " sockfd ", struct mmsghdr *" msgvec \
37 ", unsigned int " vlen ","
38 .BI " int " flags ");"
43 system call is an extension of
45 that allows the caller to transmit multiple messages on a socket
46 using a single system call.
47 (This has performance benefits for some applications.)
48 .\" See commit 228e548e602061b08ee8e8966f567c12aa079682
52 argument is the file descriptor of the socket
53 on which data is to be transmitted.
57 argument is a pointer to an array of
60 The size of this array is specified in
65 structure is defined in
72 struct msghdr msg_hdr; /* Message header */
73 unsigned int msg_len; /* Number of bytes transmitted */
82 structure, as described in
86 field is used to return the number of bytes sent from the message in
88 (i.e., the same as the return value from a single
94 argument contains flags ORed together.
95 The flags are the same as for
102 messages have been sent.
103 A nonblocking call sends as many messages as possible
104 (up to the limit specified by
106 and returns immediately.
112 fields of successive elements of
114 are updated to contain the number of bytes transmitted from the corresponding
116 The return value of the call indicates the number of elements of
118 that have been updated.
122 returns the number of messages sent from
126 the caller can retry with a further
128 call to send the remaining messages.
130 On error, \-1 is returned, and
132 is set to indicate the error.
136 An error is returned only if no datagrams could be sent.
138 .\" commit 728ffb86f10873aaf4abd26dde691ee40ae731fe
139 .\" ... only return an error if no datagrams could be sent.
140 .\" If less than the requested number of messages were sent, the application
141 .\" must retry starting at the first failed one and if the problem is
142 .\" persistent the error will be returned.
144 .\" This matches the behavior of other syscalls like read/write - it
145 .\" is not an error if less than the requested number of elements are sent.
149 system call was added in Linux 3.0.
150 Support in glibc was added in version 2.14.
155 The value specified in
160 .\" commit 98382f419f32d2c12d021943b87dea555677144b
161 .\" net: Cap number of elements for sendmmsg
163 .\" To limit the amount of time we can spend in sendmmsg, cap the
164 .\" number of elements to UIO_MAXIOV (currently 1024).
166 .\" For error handling an application using sendmmsg needs to retry at
167 .\" the first unsent message, so capping is simpler and requires less
168 .\" application logic than returning EINVAL.
170 If an error occurs after at least one message has been sent,
171 the call succeeds, and returns the number of messages sent.
172 The error code is lost.
173 The caller can retry the transmission,
174 starting at the first failed message, but there is no guarantee that,
175 if an error is returned, it will be the same as the one that was lost
176 on the previous call.
178 The example below uses
184 in two distinct UDP datagrams using one system call.
185 The contents of the first datagram originates from a pair of buffers.
189 #include <netinet/ip.h>
193 #include <sys/types.h>
194 #include <sys/socket.h>
200 struct sockaddr_in addr;
201 struct mmsghdr msg[2];
202 struct iovec msg1[2], msg2;
205 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
211 addr.sin_family = AF_INET;
212 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
213 addr.sin_port = htons(1234);
214 if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == \-1) {
219 memset(msg1, 0, sizeof(msg1));
220 msg1[0].iov_base = "one";
222 msg1[1].iov_base = "two";
225 memset(&msg2, 0, sizeof(msg2));
226 msg2.iov_base = "three";
229 memset(msg, 0, sizeof(msg));
230 msg[0].msg_hdr.msg_iov = msg1;
231 msg[0].msg_hdr.msg_iovlen = 2;
233 msg[1].msg_hdr.msg_iov = &msg2;
234 msg[1].msg_hdr.msg_iovlen = 1;
236 retval = sendmmsg(sockfd, msg, 2, 0);
238 perror("sendmmsg()");
240 printf("%d messages sent\\n", retval);