sendto

This function sends data to a specific destination.

At a Glance

Header file: Winsock.h
Windows CE versions: 1.0 and later

Syntax

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

Parameters

s

[in] Descriptor that identifies a (possibly connected) socket.

buf

[in] Buffer that contains the data to be transmitted.

len

[in] Length of the data in buf.

flags

[in] Specifies the way in which the call is made.

to

[in] Optional pointer to the address of the target socket.

tolen

[in] Size of the address in to.

Return Values

The total number of bytes sent, which can be less than the number indicated by len, indicates that no error occurred. SOCKET_ERROR indicates failure. To get a specific error value, call WSAGetLastError.

Remarks

The sendto function is used to write outgoing data on a socket. For message-oriented sockets, care must be taken not to exceed the maximum packet size of the underlying subnets, which can be obtained by using getsockopt to retrieve the value of socket option SO_MAX_MSG_SIZE. If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned and no data is transmitted.

The to parameter can be any valid address in the socket's address family, including a broadcast or any multicast address. To send to a broadcast address, an application must have used setsockopt with SO_BROADCAST enabled. Otherwise, sendto will fail with the error value WSAEACCES. For TCP/IP, an application can send to any multicast address (without becoming a group member).

If the socket is unbound, unique values are assigned to the local association by the system, and the socket is then marked as bound. An application can use getsockname to determine the local socket name in this case.

The successful completion of a sendto does not indicate that the data was successfully delivered.

The sendto function is normally used on a connectionless socket to send a datagram to a specific peer socket identified by the to parameter. Even if the connectionless socket has been previously connected to a specific address, the to parameter overrides the destination address for that particular datagram only. On a connection-oriented socket, the to and tolen parameters are ignored, making sendto equivalent to send.

For Sockets Using IP (Version 4)

To send a broadcast (on a SOCK_DGRAM only), the address in the to parameter should be constructed using the special IP address INADDR_BROADCAST (defined in Winsock.h), together with the intended port number. It is generally inadvisable for a broadcast datagram to exceed the size at which fragmentation can occur, which implies that the data portion of the datagram (excluding headers) should not exceed 512 bytes.

If no buffer space is available within the transport system to hold the data to be transmitted, sendto will block unless the socket has been placed in a nonblocking mode. On nonblocking, stream oriented sockets, the number of bytes written can be between 1 and the requested length, depending on buffer availability on both the client and server systems. The select function can be used to determine when it is possible to send more data.

Calling sendto with a len of zero is permissible and will return zero as a valid value. For message-oriented sockets, a zero-length transport datagram is sent.

The flags parameter can be used to influence the behavior of the function invocation beyond the options specified for the associated socket. The semantics of this function are determined by the socket options and the flags parameter. Windows CE supports the following flags parameter:

MSG_DONTROUTE

Specifies that the data should not be subject to routing. A Windows Sockets service provider can choose to ignore this flag.

See Also

recv, recvfrom, socket, select, send, shutdown, WSAStartup