1SENDMMSG(2) Linux Programmer's Manual SENDMMSG(2)
2
3
4
6 sendmmsg - send multiple messages on a socket
7
9 #define _GNU_SOURCE
10 #include <sys/socket.h>
11
12 int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
13 unsigned 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.
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 The example below uses sendmmsg() to send onetwo and three in two dis‐
73 tinct UDP datagrams using one system call. The contents of the first
74 datagram originates from a pair of buffers.
75
76 #define _GNU_SOURCE
77 #include <netinet/ip.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <sys/types.h>
82 #include <sys/socket.h>
83
84 int
85 main(void)
86 {
87 int sockfd;
88 struct sockaddr_in sa;
89 struct mmsghdr msg[2];
90 struct iovec msg1[2], msg2;
91 int retval;
92
93 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
94 if (sockfd == -1) {
95 perror("socket()");
96 exit(EXIT_FAILURE);
97 }
98
99 sa.sin_family = AF_INET;
100 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
101 sa.sin_port = htons(1234);
102 if (connect(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {
103 perror("connect()");
104 exit(EXIT_FAILURE);
105 }
106
107 memset(msg1, 0, sizeof(msg1));
108 msg1[0].iov_base = "one";
109 msg1[0].iov_len = 3;
110 msg1[1].iov_base = "two";
111 msg1[1].iov_len = 3;
112
113 memset(&msg2, 0, sizeof(msg2));
114 msg2.iov_base = "three";
115 msg2.iov_len = 5;
116
117 memset(msg, 0, sizeof(msg));
118 msg[0].msg_hdr.msg_iov = msg1;
119 msg[0].msg_hdr.msg_iovlen = 2;
120
121 msg[1].msg_hdr.msg_iov = &msg2;
122 msg[1].msg_hdr.msg_iovlen = 1;
123
124 retval = sendmmsg(sockfd, msg, 2, 0);
125 if (retval == -1)
126 perror("sendmmsg()");
127 else
128 printf("%d messages sent\n", retval);
129
130 exit(0);
131 }
132
134 recvmmsg(2), sendmsg(2), socket(2), socket(7)
135
137 This page is part of release 3.53 of the Linux man-pages project. A
138 description of the project, and information about reporting bugs, can
139 be found at http://www.kernel.org/doc/man-pages/.
140
141
142
143Linux 2012-12-16 SENDMMSG(2)