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 2016-03-15 "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 " unsigned 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.
137 .\" commit 728ffb86f10873aaf4abd26dde691ee40ae731fe
138 .\" ... only return an error if no datagrams could be sent.
139 .\" If less than the requested number of messages were sent, the application
140 .\" must retry starting at the first failed one and if the problem is
141 .\" persistent the error will be returned.
143 .\" This matches the behavior of other syscalls like read/write - it
144 .\" is not an error if less than the requested number of elements are sent.
148 system call was added in Linux 3.0.
149 Support in glibc was added in version 2.14.
154 The value specified in
159 .\" commit 98382f419f32d2c12d021943b87dea555677144b
160 .\" net: Cap number of elements for sendmmsg
162 .\" To limit the amount of time we can spend in sendmmsg, cap the
163 .\" number of elements to UIO_MAXIOV (currently 1024).
165 .\" For error handling an application using sendmmsg needs to retry at
166 .\" the first unsent message, so capping is simpler and requires less
167 .\" application logic than returning EINVAL.
169 The example below uses
175 in two distinct UDP datagrams using one system call.
176 The contents of the first datagram originates from a pair of buffers.
180 #include <netinet/ip.h>
184 #include <sys/types.h>
185 #include <sys/socket.h>
191 struct sockaddr_in addr;
192 struct mmsghdr msg[2];
193 struct iovec msg1[2], msg2;
196 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
202 addr.sin_family = AF_INET;
203 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
204 addr.sin_port = htons(1234);
205 if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == \-1) {
210 memset(msg1, 0, sizeof(msg1));
211 msg1[0].iov_base = "one";
213 msg1[1].iov_base = "two";
216 memset(&msg2, 0, sizeof(msg2));
217 msg2.iov_base = "three";
220 memset(msg, 0, sizeof(msg));
221 msg[0].msg_hdr.msg_iov = msg1;
222 msg[0].msg_hdr.msg_iovlen = 2;
224 msg[1].msg_hdr.msg_iov = &msg2;
225 msg[1].msg_hdr.msg_iovlen = 1;
227 retval = sendmmsg(sockfd, msg, 2, 0);
229 perror("sendmmsg()");
231 printf("%d messages sent\\n", retval);