Sends gathered bytes through a socket to any other socket.
Format
#include <types.h>
#include <socket.h>
int sendmsg ( int s, struct msghdr *msg, int flags ); (BSD
Version 4.4)
int sendmsg ( int s, struct omsghdr *msg, int flags ); (BSD
Version 4.3)
1 – Arguments
s
A socket descriptor created with the socket() function.
msg
A pointer to a msghdr structure containing the message to be
sent.
The msg_iov field of the msghdr structure is used as a series of
buffers from which data is read in order until msg_iovlen bytes
have been obtained.
flags
Can be either 0 or MSG_OOB. If it is equal to MSG_OOB, the data
is sent out of band. Data can be received before other pending
data on the receiving socket if the receiver specifies a flag of
MSG_OOB.
2 – Description
This function sends the data in a msghdr structure to any other
socket.
You can use this function on any socket to send data to any named
socket. The data in the msg_iov field of the msghdr structure
is sent to the socket whose address is specified in the msg_name
field of the structure. The receiving socket gets the data using
the read(), recv(), recvfrom(), or recvmsg() function. When the
iovec array specifies more than one buffer, the data is gathered
from all specified buffers before being sent.
Normally the sendmsg() function blocks if there is no space for
the incoming data in the buffer. It waits until the buffer space
becomes available. If the socket is set to nonblocking and there
is no space for the data, the sendmsg() function fails with the
EWOULDBLOCK error.
If the message is too large to be sent in one piece, and the
socket type is SOCK_DGRAM, which requires that messages be sent
in one piece, sendmsg() fails with the EMSGSIZE error.
If the address specified is an INADDR_BROADCAST address, the
SO_BROADCAST socket option must be set and the process must
have OPER, SYSPRV, or BYPASS privilege for the I/O operation
to succeed.
A success return from sendmsg() does not guarantee that the data
has been received by the peer. All errors (except EWOULDBLOCK)
are detected locally. To determine when it is possible to send
more data, use the select() function.
Related Functions
See also read(), recv(), recvfrom(), recvmsg(), socket(), and
getsockopt().
3 – Return Values
n The number of bytes sent.
-1 Error; errno is set to indicate the error.
4 – Errors
ENOTSOCK The socket descriptor is invalid.
EFAULT An invalid user space address is specified for
an argument.
EMSGSIZE The socket requires that messages be sent
atomically, but the size of the message to be
sent makes this impossible.
EWOULDBLOCK Blocks if the system does not have enough
space for buffering the user data.