sendmmsg()

Updated: April 19, 2023

Send multiple messages to a socket

Synopsis:

#include <sys/types.h>
#include <sys/socket.h>

int sendmmsg( int s,
              struct mmsghdr * mmsg,
              unsigned int vlen,
              unsigned int flags );

Arguments:

s
The descriptor for the socket; see socket().
mmsg
A pointer to the array of messages that you want to send.
vlen
The length of the mmsg array; limited to 1024 elements.
flags
A bitwise OR of zero or more of the following:
  • MSG_DONTWAIT — if no data is available, then instead of blocking, return immediately with the error EAGAIN.
  • MSG_NOTIFICATION — if the flag is not set, then the function returns data. If a notification has arrived, then the flag is set and it returns a notification in the msg_iov field.
  • MSG_OOB — process out-of-band data. Use this bit when you send “out-of-band” data on sockets that support this notion (e.g., SOCK_STREAM). The underlying protocol must also support out-of-band data.
  • MSG_DONTROUTE — bypass routing; create a direct interface. You normally use this bit only in diagnostic or routing programs.
  • MSG_NOSIGNAL — don't raise a SIGPIPE signal when the other end breaks the connection.
  • MSG_WAITFORONE — wait for one packet or an error. This flag causes the operation to block until at least one packet is available. With this setting, you can receive as many as vlen packets, but if only one is available, then the function returns one. However, the call may return less data than available under the following scenarios:
    • a signal is caught
    • an error or disconnect occurs
    • the incoming packet is a different data type from the one previously received
    This flag turns on MSG_DONTWAIT after the first message has been received.

Library:

libsocket

Use the -l socket option to qcc to link against this library.

Description:

The sendmmsg() function transmits multiple messages to another socket. You can use send() only when the socket is in a connected state; you can use sendmmsg() at any time.

You can use this function to send multiple messages in the same call using an array of mmsghdr elements with the following form, as defined in <sys/socket.h>:

struct mmsghdr {
    struct msghdr   msg_hdr; /* the message to be sent */
    unsigned int    msg_len; /* number of bytes transmitted */
};

The msg_len member contains the number of bytes sent for each msg_hdr member. The array has vlen elements, but if there's an error, a number fewer than vlen may be returned. For a description of the msghdr structure, see recvmsg().

No indication of failure to deliver is implicit in a sendmmsg(). Locally detected errors are indicated by a return value of -1.

If no message space is available at the socket to hold the message to be transmitted, then sendmmsg() normally blocks, unless the socket has been placed in nonblocking I/O mode. You can use poll() to determine when it's possible to send more data.

Returns:

The number of messages sent, or -1 if an error occurs (errno is set).

Errors:

EACCES
Search permission is denied for a component of the path prefix, or write access to the named socket is denied.
EAFNOSUPPORT
Addresses in the specified address family can't be used with this socket.
EAGAIN
The socket's file descriptor is marked O_NONBLOCK, and the requested operation would block.
EBADF
An invalid descriptor was specified.
ECONNRESET
A connection was forcibly closed by a peer.
EDESTADDRREQ
The socket isn't connection-mode and doesn't have its peer address set, and no destination address was specified.
EFAULT
An invalid user space address was specified for a parameter.
EHOSTUNREACH
The destination host can't be reached (probably because the host is down, or a remote router can't reach it).
EINTR
A signal interrupted sendmmsg() before any data was transmitted.
EINVAL
The sum of the iov_len values overflows an ssize_t.
EISCONN
A destination address was specified and the socket is already connected.
EMSGSIZE
The message is too large to be sent all at once (as the socket requires), or the msg_iovlen member of the msghdr structure pointed to by message is less than or equal to 0 or is greater than IOV_MAX.
ENETDOWN
The local network interface used to reach the destination is down.
ENETUNREACH
No route to the network is present.
ENOBUFS
The system couldn't allocate an internal buffer. The operation may succeed when buffers become available.
ENOMEM
Insufficient memory was available to fulfill the request.
ENOTCONN
The socket is connection-mode but isn't connected.
ENOTSOCK
The argument s isn't a socket.
EOPNOTSUPP
The s argument is associated with a socket that doesn't support one or more of the values set in flags.
EOVERFLOW
An attempt was made to send an amount of data that when added to the sizes of the socket send message and socket address length structures exceeds the allowable limit.
EPIPE
The socket is shut down for writing, or the socket is connection-mode and is no longer connected. In the latter case, and if the socket is of type SOCK_STREAM, a SIGPIPE signal is generated to the calling thread.
EWOULDBLOCK
The socket is marked nonblocking and the requested operation would block.

Classification:

Unix

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes