sendto

Top  Previous  Next

The sendto function sends data to a specific destination.

If the socket is connection-less, the message is sent to the address specified by addr. If the socket is connection-oriented, addr is ignored.

Successful completion of a call to send does not guarantee delivery of the message. A return value of -1 indicates only locally-detected errors.

If space is not available at the sending socket to hold the message to be transmitted, sendto blocks until space is available.

If the socket protocol supports broadcast and the specified address is a broadcast address for the socket protocol, sendto fails if the SO_BROADCAST option is not set for the socket.

 

Syntax

ssize_t sendto(

int socket,

const void *buf,

size_t len,

int flags,

const struct sockaddr *addr,

int addrLen

);

 

Parameters

socket

The descriptor that identifies a connected socket.

 

buf

A pointer to a buffer containing the data to be transmitted.

 

len

The length, in bytes, of the buffer pointed to by the buf parameter. If the message is too long to pass through the underlying protocol, send fails and no data is transmitted.

 

flags

A set of flags that influences the behavior of this function. This parameter is constructed by using the bitwise OR operator with any of the following values.

 

Type

Meaning

MSG_OOB

Sends out-of-band data in sockets that support out of band communications. The significance and semantics of out-of band data are protocol-specific.

MSG_WAITALL

Terminates a record (if supported by the protocol).

 

addr

An optional pointer to a sockaddr structure that contains the address of the target socket. The length and format of the address depend on the address family of the socket.

 

addrLen

The size, in bytes, of the address pointed to by the to parameter.

 

Return value

Upon successful completion, sendto returns the number of bytes sent. Otherwise, -1 is returned and errno set to indicate the error.