sendmsg

Sends gathered bytes through a socket to any other socket.

Format

#include  <socket.h>

int sendmsg  (int s, struct msghdr msg[], int
             flags);
Routine Variants This socket routine has a variant named __bsd44_sendmsg. Enabled by defining _SOCKADDR_LEN, this variant implements 4.4BSD-compatible semantics. See Section A.7 for more information.

Arguments

s
A socket descriptor that has been created with socket.
msg
A pointer to a msghdr structure containing the message to be sent. See <socket.h> for a description of the msghdr structure.

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
May be either 0 or MSG_OOB. If it is equal to MSG_OOB, the data will be sent out-of-band. This means that the data can be received before other pending data on the receiving socket if the receiver also specifies a flag of MSG_OOB.

Description

This routine may be used on any socket to send data to any named socket. The data in the msg_iovec field of the msg 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 either the read, recv, recvfrom, or recvmsg routine. When the iovec array specifies more than one buffer, the data is gathered from all specified buffers before being sent. See <uio.h> for a description of the iovec structure.

If there is no space available to buffer the data being sent on the receiving end of the connection, sendmsg will normally block until buffer space becomes available. If the socket is defined as nonblocking, however, sendmsg will fail with an errno indication of EWOULDBLOCK. If the message is too large to be sent in one piece and the socket type requires that messages be sent atomically (SOCK_ DGRAM), sendmsg will fail with an errno indication of EMSGSIZE.

If the address specified is a INADDR_BROADCAST address, then the SO_BROADCAST option must be set and the process must have SYSPRV or BYPASS privilege for the I/O operation to succeed.

No indication of failure to deliver is implicit in a sendmsg. All errors (except EWOULDBLOCK) are detected locally. You may use the select routine to determine when it is possible to send more data.

See also read, recv, recvfrom, recvmsg, and socket in this section, and getsockopt in this appendix.

Return Values
The number of bytes sent. 
-1  Indicates an error; errno is set to one of the following:

  • EBADF - The socket descriptor is invalid.

  • ENOTSOCK - The descriptor references a file, not a socket.

  • EFAULT - An invalid user space address was specified for a parameter.

  • EMSGSIZE -The socket requires that messages be sent atomically, and the size of the message to be sent made this impossible.

  • EWOULDBLOCK - Blocks if the system does not have enough space for buffering the user data.
 


Previous Page | Next Page | Table of Contents | Index