SEND(2) | System Calls Manual | SEND(2) |
send
, sendmsg
,
sendto
— send a message from
a socket
#include
<sys/socket.h>
ssize_t
send
(int socket,
const void *buffer, size_t
length, int flags);
ssize_t
sendmsg
(int socket,
const struct msghdr *message, int
flags);
ssize_t
sendto
(int socket,
const void *buffer, size_t
length, int flags, const struct
sockaddr *dest_addr, socklen_t dest_len);
send
(),
sendto
(),
and sendmsg
() are used to transmit a message to
another socket. send
() may be used only when the
socket is in a
connected
state, while sendto
() and
sendmsg
() may be used at any time.
The address of the target is given by
dest_addr with dest_len
specifying its size. The length of the message is given by
length. If the message is too long to pass atomically
through the underlying protocol, the error EMSGSIZE
is returned, and the message is not transmitted.
No indication of failure to deliver is implicit in a
send
().
Locally detected errors are indicated by a return value of -1.
If no messages space is available at the socket to
hold the message to be transmitted, then
send
()
normally blocks, unless the socket has been placed in non-blocking I/O mode.
The select(2) call may be used to determine when it is
possible to send more data.
The flags parameter may include one or more of the following:
#define MSG_OOB 0x1 /* process out-of-band data */ #define MSG_DONTROUTE 0x4 /* bypass routing, use direct interface */
The flag MSG_OOB
is used to send
“out-of-band” data on sockets that support this notion (e.g.
SOCK_STREAM
); the underlying protocol must also
support “out-of-band” data.
MSG_DONTROUTE
is usually used only by diagnostic or
routing programs.
The
sendmsg
()
system call uses a msghdr structure to minimize the
number of directly supplied arguments. The msg_iov and
msg_iovlen fields of message specify zero or more
buffers containing the data to be sent. msg_iov points
to an array of iovec structures; msg_iovlen shall be
set to the dimension of this array. In each iovec structure, the
iov_base field specifies a storage area and the
iov_len field gives its size in bytes. Some of these
sizes can be zero. The data from each storage area indicated by
msg_iov is sent in turn. See recv(2)
for a complete description of the msghdr
structure.
Upon successful completion, the number of bytes which were sent is returned. Otherwise, -1 is returned and the global variable errno is set to indicate the error.
The send
(),
sendmsg
(), and sendto
()
system calls will fail if:
EACCES
]EAGAIN
]EBADF
]ECONNRESET
]EFAULT
]EHOSTUNREACH
]EINTR
]EMSGSIZE
]IOV_MAX
.ENETDOWN
]ENETUNREACH
]ENOBUFS
]ENOBUFS
]ENOTSOCK
]EOPNOTSUPP
]EPIPE
]EADDRNOTAVAIL
]The sendmsg
() and
sendto
() system calls will fail if:
EAFNOSUPPORT
]EDESTADDRREQ
]EISCONN
]ENOENT
]ENOMEM
]ENOTCONN
]ENOTDIR
]The send
() system call will fail if:
EDESTADDRREQ
]ENOTCONN
]The sendmsg
() system call will fail
if:
EINVAL
]EMSGSIZE
]IOV_MAX
.#include
<sys/types.h>
#include
<sys/socket.h>
The include file
<sys/types.h>
is
necessary.
fcntl(2), getsockopt(2), recv(2), select(2), socket(2), write(2), compat(5)
The send
() function call appeared in
4.2BSD.
February 21, 1994 | BSD 4.2 |