Send data on a socket.
Syntax
#include <sys/socket.h>
ssize_t write (int fd, void *buf, size_t len);
ssize_t send (int fd, void *buf, size_t len, int flags);
ssize_t sendto (int fd, void *buf, size_t len, int flags,
struct sockaddr *addr, socklen_t addrlen);
Parameters
-
fd
The socket to write to.
-
buf
The buffer containing the data to send.
-
len
The length of the data to send, in bytes.
-
flags
Option flags for
write
/send
/sendto
. Currently not used by any supported networking types. -
addr
Optional pointer to an address structure that contains the address of the receiver. The size of the structure must be specified in
addrlen
. -
addrlen
If
addr
is specified, the size of theaddr
buffer. Ifaddr
is not specified, set to null.
Return Value
The return value is the number of bytes sent if successful. Otherwise, the return value is negative and errno is set:
-
EBADF
Bad file descriptor.
-
ENOMEM
Out of memory.
-
EINVAL
Invalid argument.
Remarks
See the documentation for the correct socket address family (network stack) for details related to it.
For datagram sockets, the buffer size must be smaller or equal to the largest supported packet size. Remaining bytes will be truncated.
write
is equal to send
with the flags
argument set to zero. send
is
equal to sendto
with the addr
and addrlen
arguments set to zero.