SEND(2) — Series 300 and 800 Only
NAME
send, sendto − send a message from a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
cc = send(s, msg, len, flags)
int cc;
int s;
char *msg;
int len, flags;
cc = sendto(s, msg, len, flags, to, tolen)
int cc;
int s;
char *msg;
int len, flags;
struct sockaddr *to;
int tolen;
DESCRIPTION
Send and sendto are used to transmit a message to another socket.
S is a socket descriptor that specifies the socket on which the message will be sent. Msg points to the buffer that contains the message.
If the socket uses connection-based communications, such as a SOCK_STREAM socket, these calls can only be used after the connection has been established (see connect(2)). In this case, any destination specified by to is ignored. For connection-less sockets, such as SOCK_DGRAM, sendto must be used unless the destination address has already been specified by connect(2). If the destination address has been specified and sendto is used, an error results if any address is specified by to.
The address of the target is contained in a socket address structure pointed at by to with tolen specifying the size of the structure.
If a sendto is attempted on a SOCK_DGRAM socket before any local address has been bound to it, the system automatically selects a local address to be used for the message. In this case, there is no guarantee that the same local address will be used for successive sendto requests on the same socket.
The length of the message is given by len. The length of data actually sent is returned in cc. If the message is too long to pass atomically through the underlying protocol, the message is not transmitted, −1 is returned and errno is set to EMSGSIZE. For SOCK_DGRAM sockets, this size is fixed by the implementation (see the DEPENDENCIES section below). The same size is used for SOCK_STREAM sockets if the socket is in non-blocking mode. Otherwise there is no size limit.
No indication of failure to deliver is implicit in a send/sendto. Return values of −1 indicate some locally detected errors.
If no message space is available at the socket to hold the message to be transmitted, send blocks, unless the socket has been placed in non-blocking mode (see socket(7)). The select(2) call can be used to determine when it is possible to send more data by selecting the socket for writing.
The supported values for flags are zero, or MSG_OOB, to send 1 out of band data byte. A write(2) call made to a socket behaves in exactly the same way as send with flags set to zero.
RETURN VALUE
If successful, the call returns the number of characters sent. If the call fails, −1 is returned.
DIAGNOSTICS
The send or sendto calls fail if:
[EACCES] Process doing a send of a broadcast packet does not have super-user capability.
[EBADF] An invalid descriptor was specified.
[ENOTSOCK] The argument s is not a socket.
[EFAULT] The msg or to parameter is not a valid pointer.
[EMSGSIZE] The socket requires that messages be sent atomically, and the size of the message to be sent made this impossible.
[EWOULDBLOCK] The socket is in non-blocking mode and the requested operation would block.
[ENOBUFS] Insufficient resources were available in the system to perform the operation.
[EINTR] The operation was interrupted by a signal before any data were sent. (If some of the data were sent, cc contains the number of bytes sent before the signal and EINTR is not given.)
[EINVAL] The len or tolen parameter contains a bad value.
[EDESTADDRREQ] The to parameter needs to specify a destination address for the message. This is also given if the specified address contains unspecified fields (see inet(7F)).
[ENOTCONN] A send on a socket that is not connected, or a send on a socket that has not completed the connect sequence with its peer.
[EISCONN] An address was specified by to for a SOCK_DGRAM socket which is already connected.
[EAFNOSUPPORT] Requested address does not match the address family of this socket.
[ECONNRESET] Series 800 only: An attempt was made to send on a socket that was connected, but the connection has been shutdown either by the remote peer or by this side of the connection.
[EPIPE and SIGPIPE signal] Series 300 only: An attempt was made to send on a socket that was connected, but the connection has been shutdown either by the remote peer or by this side of the connection.
[EIO] A timeout occurred.
DEPENDENCIES
If the fcntl(2) O_NDELAY flag is set, and the window is full, a Series 800 returns 0 with no error (like a pipe) and a Series 300 blocks. If O_NDELAY is not set, and a write(2)/send(2) request is blocked, the following occurs if the remote node shuts down the connection: a Series 800 returns -1 with the ECONNRESET error and a Series 300 returns -1 with the EPIPE error and generates a SIGPIPE (like a pipe).
Series 300 and 800:
The maximum UDP message size which can be sent on a SOCK_DGRAM socket is 9216 bytes.
AUTHOR
UCB (University of California at Berkeley)
SEE ALSO
getsockopt(2), recv(2), select(2), socket(2), inet(7F), socket(7).
The SO_SNDBUF option can be set to change the send socket buffer size. Refer to the getsockopt(2) reference page for details.
The SO_DONTROUTE option can be set to indicate that outgoing messages should bypass the standard routing facilities. Refer to the getsockopt(2) reference page for details.
Hewlett-Packard Company — May 11, 2021