sendto()
PROTOTYPE
#include <sockets.h>
ssize_t sendto(int socket, void *buffer, size_t length, int flags, void *to, socklen_t tolen);
DESCRIPTION
sendto() is primarily used to send data over unconnected UDP sockets. TCP sockets and connected UDP sockets can only use sendto() if to is NULL, in which case sendto() behaves exactly like send(). Refer to the description of send() for more information.
For unconnected UDP sockets, to points to a structure (sockaddr_in or sockaddr_in6) containing the destination address. The IP address and port number must be in network byte order. tolen must be greater than or equal to the socket address size. If the socket has not been assigned a local port number, sendto() finds an unused port number and binds it to the socket.
If successful, sendto() returns the number of bytes sent. Otherwise, it sets errno and returns -1.
ERROR CODES
| EACCES | to is a broadcast address and SO_BROADCAST is not set (UDP only). |
| EAFNOSUPPORT | Address family of the destination address doesn’t match the socket’s. |
| ECONNABORTED | Connection was aborted because of unacknowledged transmissions. |
| ECONNRESET | Connection was forcibly closed by remote host (received RST). |
| EDESTADDRREQ | Either the destination IP address or port number is zero. |
| EFAULT | buffer is NULL and length is not 0. |
| EHOSTUNREACH | Timed out after receiving a “host unreachable” ICMP message. |
| EINVAL | tolen is less than the socket address size. |
| EISCONN | to is not NULL and the socket is connected. |
| EMFILE | Socket not bound to local port number and no port is free (UDP only). |
| EMSGSIZE | length is larger than maximum datagram size (UDP only). |
| ENETDOWN | TargetTCP has not been initialized. |
| ENETUNREACH | No route exists to destination network (UDP only). |
| ENOBUFS | No buffer space available for datagram (UDP only). |
| ENOTCONN | The socket is not connected and to is NULL. |
| ENOTSOCK | socket is not a valid socket handle. |
| EOPNOTSUPP | MSG_OOB specified on UDP socket. |
| ESHUTDOWN | The socket has been shutdown for sending by shutdown(). |
| ETIMEDOUT | Connection dropped because of no response from peer (TCP only). |
| EWOULDBLOCK | Socket is non-blocking and the send buffer is full (TCP only). |
EXAMPLE
int flag, rc, sock;
struct sockaddr_in addr;
/*-------------------------------------------------------------------*/
/* Create a UDP socket. */
/*-------------------------------------------------------------------*/
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) exit(errno);
/*-------------------------------------------------------------------*/
/* Set "send broadcast okay" flag. */
/*-------------------------------------------------------------------*/
flag = TRUE;
rc = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &flag, sizeof(flag));
if (rc < 0) exit(errno);
/*-------------------------------------------------------------------*/
/* Send "hello" to broadcast address. */
/*-------------------------------------------------------------------*/
bzero(&addr, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(100);
addr.sin_addr.s_addr = 0xFFFFFFFF;
sendto(sock, "hello", 5, 0, &addr, sizeof(addr));