sendto()--Send Data


Syntax
#include <sys/types.h>
#include <sys/socket.h>

int sendto(int socket_descriptor,
           char *buffer,
           int buffer_length,
           int flags,
           struct sockaddr *destination_address,
           int address_length)


Threadsafe: Yes

The sendto() function is used to send data through a connected or unconnected socket.

Parameters

socket_descriptor
(Input) The socket descriptor that is to be written to.

buffer
(Input) The pointer to the buffer in which the data that is to be written is stored.

buffer_length
(Input) The length of the buffer.

flags
(Input) A flag value that controls the transmission of the data. The flags value is either zero, or is obtained by performing an OR operation on one or more of the following constants:
MSG_OOB
Send data as out-of-band data. Valid only for sockets with an address family of AF_INET and type SOCK_STREAM.
MSG_DONTROUTE
Bypass routing. Valid only for sockets with address family of AF_NS or AF_INET. It is ignored for other address families.

destination_address
(Input) A pointer to a buffer of type struct sockaddr that contains the destination address to which the data is to be sent. The structure sockaddr is defined in <sys/socket.h>.
      struct sockaddr {
         u_short sa_family;
         char    sa_data[14];
      };

The sa_family field identifies the address family to which the address belongs, and sa_data is the address whose format is dependent on the address family.

address_length
(Input) The length of the destination_address.

Return Value

sendto() returns an integer. Possible values are:

Error Conditions

When sendto() fails, errno can be set to one of the following:

[EACCES]
Permission denied.

The process does not have the appropriate privileges to the destination address.

[EADDRNOTAVAIL]
Address not available.

A socket with an address family of AF_INET is using a connectionless transport service, and the socket was not bound. The system tried to bind the socket but could not because a port was not available.

[EBADF]
Descriptor not valid.
[ECONNREFUSED]
The destination socket refused an attempted connect operation.

This error code can only be returned on sockets that use a connectionless transport service.

[EDESTADDRREQ]
Operation requires destination address.

A destination address has not been associated with the socket pointed to by the socket_descriptor parameter and a destination address was not passed in as an argument on the sendto(). This error code can only be returned on sockets that use a connectionless transport service.

[EFAULT]
Bad address.

The system detected an address which was not valid while attempting to access the buffer or destination_address parameter.

[EHOSTDOWN]
A remote host is not available.

This error code can only be returned on sockets that use a connectionless transport service.

[EHOSTUNREACH]
A route to the remote host is not available.

This error code can only be returned on sockets that use a connectionless transport service.

[EINTR]
Interrupted function call.
[EINVAL]
Parameter not valid.

This error code indicates one of the following:

[EIO]
Input/output error.
[EISCONN]
A connection has already been established.

A destination address was set, but the socket pointed to by the socket_descriptor parameter already has a destination address associated with it.

[ELOOP]
A loop exists in symbolic links encountered during pathname resolution.

This error code refers to the destination address, and can only be returned on sockets that use the AF_UNIX address family.

[EMSGSIZE]
Message size out of range.

The data to be sent could not be sent atomically because the total size of the data to be sent is too large.

[ENAMETOOLONG]
File name too long.

This error code refers to the destination address, and can only be returned on sockets that use the AF_UNIX address family.

[ENETDOWN]
The network is not currently available.

This error code can only be returned on sockets that use a connectionless transport service.

[ENETUNREACH]
Cannot reach the destination network.

This error code can only be returned on sockets that use a connectionless transport service.

[ENOBUFS]
There is not enough buffer space for the requested operation.
[ENOENT]
No such file or directory.

This error code refers to the destination address, and can only be returned on sockets that use the AF_UNIX address family.

[ENOSYS]
Function not implemented.

This error code refers to the destination address, and can only be returned on sockets that use the AF_UNIX address family.

[ENOTCONN]
Requested operation requires a connection.

This error code can only be returned on sockets that use a connection-oriented transport service.

[ENOTDIR]
Not a directory.

This error code refers to the destination address, and can only be returned on sockets that use the AF_UNIX address family.

[ENOTSOCK]
The specified descriptor does not reference a socket.
[EOPNOTSUPP]
Operation not supported.

This error code indicates one of the following:

[EPIPE]
Broken pipe.
[EPROTOTYPE]
The socket type or protocols are not compatible.

This error code is only returned on sockets that use the AF_UNIX address family.

[EUNATCH]
The protocol required to support the specified address family is not available at this time.
[EUNKNOWN]
Unknown system state.
[EWOULDBLOCK]
Operation would have caused the thread to be suspended.

Error Messages

CPE3418 E
Possible APAR condition or hardware failure.
CPF9872 E
Program or service program &1 in library &2 ended. Reason code &3.
CPFA081 E
Unable to set return value or error code.

Usage Notes

  1. A destination address cannot be specified if the socket pointed to by the socket_descriptor parameter already has a destination address associated with it. To not specify an address, users must set the destination_address field to NULL or set the address_length field to zero. (Not specifying an address by setting the address_length field to zero is an IBM extension.)

    Note: The destination_address and address_length fields are ignored if the socket is using a connection-oriented transport service.

  2. If the socket is using a connectionless transport device, the socket is not bound to an address, and the socket type is SOCK_DGRAM, the system automatically selects an address (INADDR_ANY and an available port number) and binds it to the socket before sending the data.

  3. To broadcast on an AF_INET socket, the socket option SO_BROADCAST must be set (with a setsockopt()).

  4. When using a connection-oriented transport service, all errors except [EUNATCH] and [EUNKNOWN] are mapped to [EPIPE] on an output operation when either of the following occurs: To get the actual error, use getsockopt() with the SO_ERROR option, or perform an input operation (for example, read()).

  5. If the socket is using an address family of AF_UNIX, the destination address (which is a path name) is assumed to be in the default coded character set identifier (CCSID) currently in effect for the job.

Related Information


Top | Sockets APIs | APIs by category


[Information Center Home Page | Feedback ] [Legal | AS/400 Glossary]