sendto

sendto transmits a message on a non-connected non-TCP socket.

Syntax

int sendto(
    SOCKET s,
    const char *buf,
    int len,
    int flags,
    const struct sockaddr *to,
    int tolen
);

Parameters

s

Socket descriptor for a connected socket.

buf

A pointer to a character buffer that holds the data to send.

len

An integer containing the size, in bytes, of the character buffer buf.

flags

A set of flags arguments that is logical OR of one or more of the following.

Flag Description

MSG_DONTROUTE

Send without using routing tables (not currently supported).

MSG_OOB

Process urgent data, SOCK_STREAM only.


to

Pointer to the sockaddr structure of the target host.

tolen

Size of the sockaddr structure associated with to.

Return Value

The number of bytes sent if the function succeeds, SOCKET_ERROR if the function fails

Use WSAGetLastError to retrieve a specific error code.

Remarks

The send and sendto functions are used to transmit a message to another socket. The send function can only be used when the socket is in a connected state. The sendto function includes the additional parameters to, which specifies a target address and tolen, which specifies the address size.

At present, sendto only works with an unconnected socket and requires the address pointer to be non-NULL and address length to be >= of sizeof(struct sockaddr_in) or >= sizeof(struct sockaddr_in6).

You must set Scope ID when sending data to a link-local IPv6 address. Otherwise, sendto will fail.

If the message is too long to pass through the underlying protocol and the protocol does not support fragmentation, SOCKET_ERROR is returned and WSAEMSGSIZE is the last error set.

Sending UDP Datagrams to a Non-existent Host

When sendto is called with the IP address of the host with no ARP entry, TCP/IP stack sends the ARP request and can send an IP packet only if the ARP reply has been received. The packets intended to be sent to the host are waiting in the socket send queue. If the send socket queue is out of space, sendto will block on a blocking socket and return SOCKET_ERROR with WSAEWOULDBLOCK error code on a non-blocking socket. If ARP reply times out, the waiting packets are discarded.

The space in a send socket queue is controlled by the maximum number of application data bytes and maximum number of datagrams.

The maximum size of socket buffer data bytes is controlled by the socket options SO_SNDBUF for send SO_RCVBUF for recv. The default value for both is 8192.

The maximum size of datagrams is controlled by custom socket options of SOL_SOCKET level SO_SND_DGRAMS for send and SO_RCV_DGRAMS for recv. The default value for both is 1024. SO_SND_DGRAMS and SO_RCV_DGRAMS must be defined in the application as:

#define SO_SND_DGRAMS 0x100B

#define SO_RCV_DGRAMS 0x100C

If blocking behavior is not desirable, an application should not use blocking sockets.

Requirements

Minimum Supported Version RTX64 2013
Header Winsock2.h
Library RtTcpip.lib