sendto Subroutine

Purpose

Sends messages through a socket.

Library

Standard C Library (libc.a)

Syntax

#include <sys/socket.h>

int sendto
(Socket, Message, Length,
Flags, To, ToLength)
int  Socket;
const void * Message;
size_t  Length;
int  Flags;
const struct sockaddr * To;
socklen_t  ToLength;

Description

The sendto subroutine allows an application program to send messages through an unconnected socket by specifying a destination address.

To broadcast on a socket, first issue a setsockopt subroutine using the SO_BROADCAST option to gain broadcast permissions.

Provide the address of the target using the To parameter. Specify the length of the message with the Length parameter. If the message is too long to pass through the underlying protocol, the error EMSGSIZE is returned and the message is not transmitted.

If the sending socket has no space to hold the message to be transmitted, the sendto subroutine blocks the message unless the socket is in a nonblocking I/O mode.

Use the select subroutine to determine when it is possible to send more data.

The socket applications can be compiled with COMPAT_43 defined. This will make the sockaddr structure BSD 4.3 compatible. For more details refer to socket.h.

Parameters

Item Description
Socket Specifies the unique name for the socket.
Message Specifies the address containing the message to be sent.
Length Specifies the size of the message in bytes.
Flags Allows the sender to control the message transmission. The Flags parameter used to send a call is formed by logically ORing one or both of the following values:
MSG_OOB
Processes out-of-band data on sockets that support SOCK_STREAM.
Note:
MSG_DONTROUTE
Sends without using routing tables.

The /usr/include/sys/socket.h file defines the Flags parameter.

To Specifies the destination address for the message. The destination address is a sockaddr structure defined in the /usr/include/sys/socket.h file.
ToLength Specifies the size of the destination address.

Return Values

Upon successful completion, the sendto subroutine returns the number of characters sent.

If the sendto subroutine is unsuccessful, the system returns a value of -1, and the errno global variable is set to indicate the error.

Error Codes

The subroutine is unsuccessful if any of the following errors occurs:

Error Description
EACCES Write access to the named socket is denied, or the socket trying to send a broadcast packet does not have broadcast capability.
EADDRNOTAVAIL The specified address is not a valid address.
EAFNOSUPPORT The specified address is not a valid address for the address family of this socket.
EBADF The Socket parameter is not valid.
ECONNRESET A connection was forcibly closed by a peer.
EDESTADDRREQ The socket is not in connection-mode and no peer address is set.
EFAULT The Address parameter is not in a writable part of the user address space.
EHOSTUNREACH The destination host cannot be reached.
EINTR A signal interrupted sendto before any data was transmitted.
EINVAL The Length or ToLength parameter is invalid.
EISCONN A SOCK_DGRAM socket is already connected.
EMSGSIZE The message is too large to be sent all at once as the socket requires.
ENETUNREACH The destination network is not reachable.
ENOBUFS The system ran out of memory for an internal data structure.
ENOENT The path name does not name an existing file, or the path name is an empty string.
ENOMEM The available data space in memory is not large enough to hold group/ACL information.
ENOPROTOOPT The protocol is not 64-bit supported.
ENOTCONN The socket is in connection-mode but is not connected.
ENOTSOCK The Socket parameter refers to a file, not a socket.
EOPNOTSUPP The socket argument is associated with a socket that does not support one or more of the values set in Flags.
EPIPE An attempt was made to send on a socket that was connected, but the connection has been shut down either by the remote peer or by this side of the connection. If the socket is of type SOCK_STREAM, the SIGPIPE signal is generated to the calling process.
EWOULDBLOCK The socket is marked nonblocking, and no connections are present to be accepted.