1sendmmsg(2)                   System Calls Manual                  sendmmsg(2)
2
3
4

NAME

6       sendmmsg - send multiple messages on a socket
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #define _GNU_SOURCE         /* See feature_test_macros(7) */
13       #include <sys/socket.h>
14
15       int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
16                    int flags);
17

DESCRIPTION

19       The  sendmmsg()  system  call is an extension of sendmsg(2) that allows
20       the caller to transmit multiple messages on a  socket  using  a  single
21       system call.  (This has performance benefits for some applications.)
22
23       The  sockfd argument is the file descriptor of the socket on which data
24       is to be transmitted.
25
26       The msgvec argument is a pointer to an  array  of  mmsghdr  structures.
27       The size of this array is specified in vlen.
28
29       The mmsghdr structure is defined in <sys/socket.h> as:
30
31           struct mmsghdr {
32               struct msghdr msg_hdr;  /* Message header */
33               unsigned int  msg_len;  /* Number of bytes transmitted */
34           };
35
36       The  msg_hdr  field  is a msghdr structure, as described in sendmsg(2).
37       The msg_len field is used to return the number of bytes sent  from  the
38       message  in  msg_hdr  (i.e., the same as the return value from a single
39       sendmsg(2) call).
40
41       The flags argument contains flags ORed together.   The  flags  are  the
42       same as for sendmsg(2).
43
44       A  blocking  sendmmsg() call blocks until vlen messages have been sent.
45       A nonblocking call sends as many messages as possible (up to the  limit
46       specified by vlen) and returns immediately.
47
48       On return from sendmmsg(), the msg_len fields of successive elements of
49       msgvec are updated to contain the number of bytes transmitted from  the
50       corresponding msg_hdr.  The return value of the call indicates the num‐
51       ber of elements of msgvec that have been updated.
52

RETURN VALUE

54       On success, sendmmsg() returns the number of messages sent from msgvec;
55       if  this  is  less than vlen, the caller can retry with a further send‐
56       mmsg() call to send the remaining messages.
57
58       On error, -1 is returned, and errno is set to indicate the error.
59

ERRORS

61       Errors are as for sendmsg(2).  An error is returned only  if  no  data‐
62       grams could be sent.  See also BUGS.
63

STANDARDS

65       Linux.
66

HISTORY

68       Linux 3.0, glibc 2.14.
69

NOTES

71       The value specified in vlen is capped to UIO_MAXIOV (1024).
72

BUGS

74       If  an  error occurs after at least one message has been sent, the call
75       succeeds, and returns the number of messages sent.  The error  code  is
76       lost.   The  caller  can  retry the transmission, starting at the first
77       failed message, but there is no guarantee that,  if  an  error  is  re‐
78       turned,  it  will  be the same as the one that was lost on the previous
79       call.
80

EXAMPLES

82       The example below uses sendmmsg() to send onetwo and three in two  dis‐
83       tinct  UDP  datagrams using one system call.  The contents of the first
84       datagram originates from a pair of buffers.
85
86       #define _GNU_SOURCE
87       #include <arpa/inet.h>
88       #include <netinet/in.h>
89       #include <stdio.h>
90       #include <stdlib.h>
91       #include <string.h>
92       #include <sys/socket.h>
93       #include <sys/types.h>
94
95       int
96       main(void)
97       {
98           int                 retval;
99           int                 sockfd;
100           struct iovec        msg1[2], msg2;
101           struct mmsghdr      msg[2];
102           struct sockaddr_in  addr;
103
104           sockfd = socket(AF_INET, SOCK_DGRAM, 0);
105           if (sockfd == -1) {
106               perror("socket()");
107               exit(EXIT_FAILURE);
108           }
109
110           addr.sin_family = AF_INET;
111           addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
112           addr.sin_port = htons(1234);
113           if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
114               perror("connect()");
115               exit(EXIT_FAILURE);
116           }
117
118           memset(msg1, 0, sizeof(msg1));
119           msg1[0].iov_base = "one";
120           msg1[0].iov_len = 3;
121           msg1[1].iov_base = "two";
122           msg1[1].iov_len = 3;
123
124           memset(&msg2, 0, sizeof(msg2));
125           msg2.iov_base = "three";
126           msg2.iov_len = 5;
127
128           memset(msg, 0, sizeof(msg));
129           msg[0].msg_hdr.msg_iov = msg1;
130           msg[0].msg_hdr.msg_iovlen = 2;
131
132           msg[1].msg_hdr.msg_iov = &msg2;
133           msg[1].msg_hdr.msg_iovlen = 1;
134
135           retval = sendmmsg(sockfd, msg, 2, 0);
136           if (retval == -1)
137               perror("sendmmsg()");
138           else
139               printf("%d messages sent\n", retval);
140
141           exit(0);
142       }
143

SEE ALSO

145       recvmmsg(2), sendmsg(2), socket(2), socket(7)
146
147
148
149Linux man-pages 6.04              2023-03-30                       sendmmsg(2)
Impressum