sendmsg()

send a message and its header to a socket

Synopsis:

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

int sendmsg( int s, 
             const struct msghdr *msg, 
             int flags );

Description:

The sendmsg() function is used to transmit a message to another socket. The send() function can be used only when the socket is in a connected state, the sendmsg() function can be used at any time.

The length of the message is given by len. If the message is too long to pass atomically through the underlying protocol, the error EMSGSIZE is returned, and the message isn't transmitted.

No indication of failure to deliver is implicit in a sendmsg(). 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 sendmsg() normally blocks, unless the socket has been placed in nonblocking I/O mode. The select() call may be used to determine when it's possible to send more data.

The flags parameter may include one or more of the following:

MSG_OOB
Process out-of-band data.
MSG_DONTROUTE
Bypass routing, direct interface.

The flag MSG_OOB is used to 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 is normally used only by diagnostic or routing programs.

See recvmsg() for a description of the msghdr structure.

Returns:

The number of characters sent. If an error occurs, -1 is returned and errno is set.

Errors:

If an error occurred, errno could contain one of the following:

EBADF
An invalid descriptor was specified.
EDESTADDRREQ
A destination address is required.
EFAULT
An invalid user space address was specified for a parameter.
EMSGSIZE
The socket requires that the message be sent atomically, but the size of the message made this impossible.
ENOBUFS
The system couldn't allocate an internal buffer. The operation may succeed when buffers become available.
ENOTSOCK
The argument s isn't a socket.
EWOULDBLOCK
The socket is marked nonblocking and the requested operation would block.

Classification:

POSIX 1003.1g (draft)

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

getsockopt(), ioctl(), recv(), send(), sendto(), socket()

select(), write() in the C Library Reference