340 lines
8.6 KiB
HTML
340 lines
8.6 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of SENDMMSG</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>SENDMMSG</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
sendmmsg - send multiple messages on a socket
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#define _GNU_SOURCE</B> /* See <A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A>(7) */
|
|
<B>#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>></B>
|
|
|
|
<B>int sendmmsg(int </B><I>sockfd</I><B>, struct mmsghdr *</B><I>msgvec</I><B>, unsigned int </B><I>vlen</I><B>,</B>
|
|
<B> int </B><I>flags</I><B>);</B>
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>sendmmsg</B>()
|
|
|
|
system call is an extension of
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2)
|
|
|
|
that allows the caller to transmit multiple messages on a socket
|
|
using a single system call.
|
|
(This has performance benefits for some applications.)
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>sockfd</I>
|
|
|
|
argument is the file descriptor of the socket
|
|
on which data is to be transmitted.
|
|
<P>
|
|
|
|
The
|
|
<I>msgvec</I>
|
|
|
|
argument is a pointer to an array of
|
|
<I>mmsghdr</I>
|
|
|
|
structures.
|
|
The size of this array is specified in
|
|
<I>vlen</I>.
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>mmsghdr</I>
|
|
|
|
structure is defined in
|
|
<I><<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>></I>
|
|
|
|
as:
|
|
<P>
|
|
|
|
|
|
|
|
struct mmsghdr {
|
|
<BR> struct msghdr msg_hdr; /* Message header */
|
|
<BR> unsigned int msg_len; /* Number of bytes transmitted */
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>msg_hdr</I>
|
|
|
|
field is a
|
|
<I>msghdr</I>
|
|
|
|
structure, as described in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2).
|
|
|
|
The
|
|
<I>msg_len</I>
|
|
|
|
field is used to return the number of bytes sent from the message in
|
|
<I>msg_hdr</I>
|
|
|
|
(i.e., the same as the return value from a single
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2)
|
|
|
|
call).
|
|
<P>
|
|
|
|
The
|
|
<I>flags</I>
|
|
|
|
argument contains flags ORed together.
|
|
The flags are the same as for
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2).
|
|
|
|
<P>
|
|
|
|
A blocking
|
|
<B>sendmmsg</B>()
|
|
|
|
call blocks until
|
|
<I>vlen</I>
|
|
|
|
messages have been sent.
|
|
A nonblocking call sends as many messages as possible
|
|
(up to the limit specified by
|
|
<I>vlen</I>)
|
|
|
|
and returns immediately.
|
|
<P>
|
|
|
|
On return from
|
|
<B>sendmmsg</B>(),
|
|
|
|
the
|
|
<I>msg_len</I>
|
|
|
|
fields of successive elements of
|
|
<I>msgvec</I>
|
|
|
|
are updated to contain the number of bytes transmitted from the corresponding
|
|
<I>msg_hdr</I>.
|
|
|
|
The return value of the call indicates the number of elements of
|
|
<I>msgvec</I>
|
|
|
|
that have been updated.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success,
|
|
<B>sendmmsg</B>()
|
|
|
|
returns the number of messages sent from
|
|
<I>msgvec</I>;
|
|
|
|
if this is less than
|
|
<I>vlen</I>,
|
|
|
|
the caller can retry with a further
|
|
<B>sendmmsg</B>()
|
|
|
|
call to send the remaining messages.
|
|
<P>
|
|
|
|
On error, -1 is returned, and
|
|
<I>errno</I>
|
|
|
|
is set to indicate the error.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
Errors are as for
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2).
|
|
|
|
An error is returned only if no datagrams could be sent.
|
|
See also BUGS.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAG"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
The
|
|
<B>sendmmsg</B>()
|
|
|
|
system call was added in Linux 3.0.
|
|
Support in glibc was added in version 2.14.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
<B>sendmmsg</B>()
|
|
|
|
is Linux-specific.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
The value specified in
|
|
<I>vlen</I>
|
|
|
|
is capped to
|
|
<B>UIO_MAXIOV</B>
|
|
|
|
(1024).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
If an error occurs after at least one message has been sent,
|
|
the call succeeds, and returns the number of messages sent.
|
|
The error code is lost.
|
|
The caller can retry the transmission,
|
|
starting at the first failed message, but there is no guarantee that,
|
|
if an error is returned, it will be the same as the one that was lost
|
|
on the previous call.
|
|
<A NAME="lbAK"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The example below uses
|
|
<B>sendmmsg</B>()
|
|
|
|
to send
|
|
<I>onetwo</I>
|
|
|
|
and
|
|
<I>three</I>
|
|
|
|
in two distinct UDP datagrams using one system call.
|
|
The contents of the first datagram originates from a pair of buffers.
|
|
<P>
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
#include <<A HREF="file:///usr/include/netinet/ip.h">netinet/ip.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/socket.h">sys/socket.h</A>>
|
|
<P>
|
|
int
|
|
main(void)
|
|
{
|
|
<BR> int sockfd;
|
|
<BR> struct sockaddr_in addr;
|
|
<BR> struct mmsghdr msg[2];
|
|
<BR> struct iovec msg1[2], msg2;
|
|
<BR> int retval;
|
|
<P>
|
|
<BR> sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
<BR> if (sockfd == -1) {
|
|
<BR> perror("socket()");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> addr.sin_family = AF_INET;
|
|
<BR> addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
<BR> addr.sin_port = htons(1234);
|
|
<BR> if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
|
|
<BR> perror("connect()");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> memset(msg1, 0, sizeof(msg1));
|
|
<BR> msg1[0].iov_base = "one";
|
|
<BR> msg1[0].iov_len = 3;
|
|
<BR> msg1[1].iov_base = "two";
|
|
<BR> msg1[1].iov_len = 3;
|
|
<P>
|
|
<BR> memset(&msg2, 0, sizeof(msg2));
|
|
<BR> msg2.iov_base = "three";
|
|
<BR> msg2.iov_len = 5;
|
|
<P>
|
|
<BR> memset(msg, 0, sizeof(msg));
|
|
<BR> msg[0].msg_hdr.msg_iov = msg1;
|
|
<BR> msg[0].msg_hdr.msg_iovlen = 2;
|
|
<P>
|
|
<BR> msg[1].msg_hdr.msg_iov = &msg2;
|
|
<BR> msg[1].msg_hdr.msg_iovlen = 1;
|
|
<P>
|
|
<BR> retval = sendmmsg(sockfd, msg, 2, 0);
|
|
<BR> if (retval == -1)
|
|
<BR> perror("sendmmsg()");
|
|
<BR> else
|
|
<BR> printf("%d messages sent\n", retval);
|
|
<P>
|
|
<BR> exit(0);
|
|
}
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+recvmmsg">recvmmsg</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sendmsg">sendmsg</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+socket">socket</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+socket">socket</A></B>(7)
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="5"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="6"><A HREF="#lbAG">VERSIONS</A><DD>
|
|
<DT id="7"><A HREF="#lbAH">CONFORMING TO</A><DD>
|
|
<DT id="8"><A HREF="#lbAI">NOTES</A><DD>
|
|
<DT id="9"><A HREF="#lbAJ">BUGS</A><DD>
|
|
<DT id="10"><A HREF="#lbAK">EXAMPLE</A><DD>
|
|
<DT id="11"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="12"><A HREF="#lbAM">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:34 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|