1SENDMMSG(2) Linux Programmer's Manual SENDMMSG(2)
2
3
4
6 sendmmsg - send multiple messages on a socket
7
9 #define _GNU_SOURCE /* See feature_test_macros(7) */
10 #include <sys/socket.h>
11
12 int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
13 int flags);
14
16 The sendmmsg() system call is an extension of sendmsg(2) that allows
17 the caller to transmit multiple messages on a socket using a single
18 system call. (This has performance benefits for some applications.)
19
20 The sockfd argument is the file descriptor of the socket on which data
21 is to be transmitted.
22
23 The msgvec argument is a pointer to an array of mmsghdr structures.
24 The size of this array is specified in vlen.
25
26 The mmsghdr structure is defined in <sys/socket.h> as:
27
28 struct mmsghdr {
29 struct msghdr msg_hdr; /* Message header */
30 unsigned int msg_len; /* Number of bytes transmitted */
31 };
32
33 The msg_hdr field is a msghdr structure, as described in sendmsg(2).
34 The msg_len field is used to return the number of bytes sent from the
35 message in msg_hdr (i.e., the same as the return value from a single
36 sendmsg(2) call).
37
38 The flags argument contains flags ORed together. The flags are the
39 same as for sendmsg(2).
40
41 A blocking sendmmsg() call blocks until vlen messages have been sent.
42 A nonblocking call sends as many messages as possible (up to the limit
43 specified by vlen) and returns immediately.
44
45 On return from sendmmsg(), the msg_len fields of successive elements of
46 msgvec are updated to contain the number of bytes transmitted from the
47 corresponding msg_hdr. The return value of the call indicates the num‐
48 ber of elements of msgvec that have been updated.
49
51 On success, sendmmsg() returns the number of messages sent from msgvec;
52 if this is less than vlen, the caller can retry with a further send‐
53 mmsg() call to send the remaining messages.
54
55 On error, -1 is returned, and errno is set to indicate the error.
56
58 Errors are as for sendmsg(2). An error is returned only if no data‐
59 grams could be sent. See also BUGS.
60
62 The sendmmsg() system call was added in Linux 3.0. Support in glibc
63 was added in version 2.14.
64
66 sendmmsg() is Linux-specific.
67
69 The value specified in vlen is capped to UIO_MAXIOV (1024).
70
72 If an error occurs after at least one message has been sent, the call
73 succeeds, and returns the number of messages sent. The error code is
74 lost. The caller can retry the transmission, starting at the first
75 failed message, but there is no guarantee that, if an error is
76 returned, it will be the same as the one that was lost on the previous
77 call.
78
80 The example below uses sendmmsg() to send onetwo and three in two dis‐
81 tinct UDP datagrams using one system call. The contents of the first
82 datagram originates from a pair of buffers.
83
84 #define _GNU_SOURCE
85 #include <netinet/ip.h>
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <string.h>
89 #include <sys/types.h>
90 #include <sys/socket.h>
91
92 int
93 main(void)
94 {
95 int sockfd;
96 struct sockaddr_in addr;
97 struct mmsghdr msg[2];
98 struct iovec msg1[2], msg2;
99 int retval;
100
101 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
102 if (sockfd == -1) {
103 perror("socket()");
104 exit(EXIT_FAILURE);
105 }
106
107 addr.sin_family = AF_INET;
108 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
109 addr.sin_port = htons(1234);
110 if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
111 perror("connect()");
112 exit(EXIT_FAILURE);
113 }
114
115 memset(msg1, 0, sizeof(msg1));
116 msg1[0].iov_base = "one";
117 msg1[0].iov_len = 3;
118 msg1[1].iov_base = "two";
119 msg1[1].iov_len = 3;
120
121 memset(&msg2, 0, sizeof(msg2));
122 msg2.iov_base = "three";
123 msg2.iov_len = 5;
124
125 memset(msg, 0, sizeof(msg));
126 msg[0].msg_hdr.msg_iov = msg1;
127 msg[0].msg_hdr.msg_iovlen = 2;
128
129 msg[1].msg_hdr.msg_iov = &msg2;
130 msg[1].msg_hdr.msg_iovlen = 1;
131
132 retval = sendmmsg(sockfd, msg, 2, 0);
133 if (retval == -1)
134 perror("sendmmsg()");
135 else
136 printf("%d messages sent\n", retval);
137
138 exit(0);
139 }
140
142 recvmmsg(2), sendmsg(2), socket(2), socket(7)
143
145 This page is part of release 4.16 of the Linux man-pages project. A
146 description of the project, information about reporting bugs, and the
147 latest version of this page, can be found at
148 https://www.kernel.org/doc/man-pages/.
149
150
151
152Linux 2018-02-02 SENDMMSG(2)