[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Technical Reference: Communications, Volume 2

sendto Subroutine


Sends messages through a socket.


Standard C Library (libc.a)


#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;


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.


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:
Processes out-of-band data on sockets that support SOCK_STREAM.
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
EBADF The Socket parameter is not valid.
ENOTSOCK The Socket parameter refers to a file, not a socket.
EFAULT The Address parameter is not in a writable part of the user address space.
EMSGSIZE The message is too large to be sent all at once as the socket requires.
EWOULDBLOCK The socket is marked nonblocking, and no connections are present to be accepted.

Related Information

The getsockopt subroutine, recv subroutine, recvfrom subroutine, recvmsg subroutine, select subroutine, send subroutine, sendmsg subroutine, setsockopt subroutine. shutdown subroutine, socket subroutine.

Sending UNIX Datagrams Example Program, Sending Internet Datagrams Example Program, Sockets Overview, Understanding Socket Data Transfer in AIX 5L Version 5.2 Communications Programming Concepts.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]