sendto

Writes data from a user buffer to a socket at the specified destination address.

Library:LibC
Standard:BSD
Service:Networking

Syntax

  #include <sys/socket.h> 
  
  ssize_t sendto (
    int                      s,
    const void              *msg,
    size_t                   len,
    int                      flags,
    const struct sockaddr   *to,
    size_t                   tolen);
  

Parameters

s

(IN) Specifies the socket file handle of a socket to which the data is to be written.

msg

(IN) Points to the message to be sent.

len

(IN) Always specifies the length of the message.

flags

(IN) Specifies the type of message transmission. See Message Types.

to

(IN) Points to the location of the structure containing the destination address. For IPv4, the to parameter points to a sockaddr structure; for IPv6, to a sockaddr_in6 structure.

If the socket has a connection, this parameter is ignored.

tolen

(IN) Specifies the size of the destination address.

Return Values

If successful, returns the number of characters sent. Otherwise, returns -1 and sets errno to one of the following:

Decimal

Constant

Description

4

EBADF

An invalid handle was specified.

35

EWOULDBLOCK

The socket is marked nonblocking, and the requested operation would block it.

38

ENOTSOCK

The socket parameter does not reference a socket handle.

40

EMSGSIZE

The socket requires that a message be sent atomically. The size of the message to be sent made it impossible.

55

ENOBUFS

The system could not allocate an internal buffer. The operation might succeed if retried when buffers become available.

Remarks

The sendto function can send messages to both connection and connectionless sockets.

The function can be used on SOCK_STREAM sockets. When called on a blocking SOCK_STREAM socket, the function blocks until all of the client’s data can be sent or buffered by the socket. When called on a nonblocking socket, the function sends or buffers the maximum amount of data that can be handled without blocking and returns the amount that was taken. If no data is taken, it returns -1, indicating an EWOULDBLOCK error.

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

Only TCP sockets support out-of-band transmission (accomplished by placing a new urgent boundary just before the last byte of the sent message).

If the message is too long to pass atomically through the underlying protocol, EMSGSIZE is returned in errno, and the message is not transmitted.

See Also