sendto

Sends bytes through a socket to any other socket.

Format

#include  <socket.h>

int sendto  (int s, char *msg, int len,
            int flags, struct sockaddr *to, int
            tolen);
Routine Variants This socket routine has a variant named __bsd44_sendto. Enabled by defining _SOCKADDR_LEN, this variant implements 4.4BSD-compatible semantics. See Section A.7 for more information.

Arguments

s
A socket descriptor that has been created with socket.
msg
A pointer to a buffer containing the data to be sent.
len
The length of the data pointed to by msg.
flags
May be either 0 or MSG_OOB. If it is equal to MSG_OOB, the data will be sent out-of-band. This means that the data can be received before other pending data on the receiving socket if the receiver also specifies a MSG_OOB in its flag parameter of the call.
to
Points to the address structure of the socket to which the data is to be sent.
tolen
The length of the address structure to points to.

Description

This routine may be used on any socket to send data to any named socket. The data in the msg buffer is sent to the socket whose address is specified in to, and the address of socket s is provided to the receiving socket. The receiving socket gets the data using either the read, recv, recvfrom, or recvmsg routine.

If there is no space available to buffer the data being sent on the receiving end of the connection, sendto will normally block until buffer space becomes available. If the socket is defined as nonblocking, however, sendto will fail with an errno indication of EWOULDBLOCK. If the message is too large to be sent in one piece and the socket type requires that messages be sent atomically (using SOCK_DGRAM), sendto will fail with an errno indication of EMSGSIZE.

No indication of failure to deliver is implicit in a sendto. All errors (except EWOULDBLOCK) are detected locally. You may use the select routine to determine when it is possible to send more data.

If the address specified is a INADDR_BROADCAST address, then the SO_BROADCAST option must be set and the process must have SYSPRV or BYPASS privilege for the I/O operation to succeed.

See also read, recv, recvfrom, recvmsg, and socket in this section, and getsockopt in this appendix.

Return Values
The number of bytes sent. This value will normally equal len
- 1  Indicates an error; errno is set to one of the following:

  • EBADF - The socket descriptor is invalid.

  • ENOTSOCK - The descriptor references a file, not a socket.

  • EFAULT - An invalid user space address was specified for a parameter.

  • EMSGSIZE -The socket requires that messages be sent atomically, and the size of the message to be sent made this impossible.

  • EWOULDBLOCK - Blocks if the system does not have enough space for buffering the user data.
 


Previous Page | Next Page | Table of Contents | Index