1SEND(2)                    Linux Programmer's Manual                   SEND(2)
2
3
4

NAME

6       send, sendto, sendmsg - send a message on a socket
7

SYNOPSIS

9       #include <sys/types.h>
10       #include <sys/socket.h>
11
12       ssize_t send(int sockfd, const void *buf, size_t len, int flags);
13
14       ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
15                      const struct sockaddr *dest_addr, socklen_t addrlen);
16
17       ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
18

DESCRIPTION

20       The system calls send(), sendto(), and sendmsg() are used to transmit a
21       message to another socket.
22
23       The send() call may be used only when the  socket  is  in  a  connected
24       state  (so  that the intended recipient is known).  The only difference
25       between send() and write(2) is the presence  of  flags.   With  a  zero
26       flags  argument, send() is equivalent to write(2).  Also, the following
27       call
28
29           send(sockfd, buf, len, flags);
30
31       is equivalent to
32
33           sendto(sockfd, buf, len, flags, NULL, 0);
34
35       The argument sockfd is the file descriptor of the sending socket.
36
37       If sendto() is used on a connection-mode (SOCK_STREAM,  SOCK_SEQPACKET)
38       socket,  the arguments dest_addr and addrlen are ignored (and the error
39       EISCONN may be returned when they are not NULL and 0),  and  the  error
40       ENOTCONN  is returned when the socket was not actually connected.  Oth‐
41       erwise, the address of the target is given by  dest_addr  with  addrlen
42       specifying its size.  For sendmsg(), the address of the target is given
43       by msg.msg_name, with msg.msg_namelen specifying its size.
44
45       For send() and sendto(), the message is found in  buf  and  has  length
46       len.   For  sendmsg(), the message is pointed to by the elements of the
47       array msg.msg_iov.  The sendmsg() call also  allows  sending  ancillary
48       data (also known as control information).
49
50       If  the  message  is too long to pass atomically through the underlying
51       protocol, the error EMSGSIZE is returned, and the message is not trans‐
52       mitted.
53
54       No  indication  of failure to deliver is implicit in a send().  Locally
55       detected errors are indicated by a return value of -1.
56
57       When the message does not fit into  the  send  buffer  of  the  socket,
58       send()  normally blocks, unless the socket has been placed in nonblock‐
59       ing I/O mode.  In nonblocking mode it would fail with the error  EAGAIN
60       or  EWOULDBLOCK in this case.  The select(2) call may be used to deter‐
61       mine when it is possible to send more data.
62
63   The flags argument
64       The flags argument is the bitwise OR of zero or more of  the  following
65       flags.
66
67       MSG_CONFIRM (since Linux 2.3.15)
68              Tell  the  link  layer that forward progress happened: you got a
69              successful reply from the other side.  If the link layer doesn't
70              get  this  it  will  regularly reprobe the neighbor (e.g., via a
71              unicast ARP).  Valid only on SOCK_DGRAM and SOCK_RAW sockets and
72              currently  implemented  only  for IPv4 and IPv6.  See arp(7) for
73              details.
74
75       MSG_DONTROUTE
76              Don't use a gateway to send out the packet, send to  hosts  only
77              on  directly  connected  networks.  This is usually used only by
78              diagnostic or routing programs.  This is defined only for proto‐
79              col families that route; packet sockets don't.
80
81       MSG_DONTWAIT (since Linux 2.2)
82              Enables  nonblocking  operation;  if  the operation would block,
83              EAGAIN or EWOULDBLOCK is returned.  This provides similar behav‐
84              ior  to  setting  the  O_NONBLOCK flag (via the fcntl(2) F_SETFL
85              operation), but differs  in  that  MSG_DONTWAIT  is  a  per-call
86              option,  whereas  O_NONBLOCK  is  a  setting  on  the  open file
87              description (see open(2)), which will affect all threads in  the
88              calling  process  and  as well as other processes that hold file
89              descriptors referring to the same open file description.
90
91       MSG_EOR (since Linux 2.2)
92              Terminates a record (when this notion is supported, as for sock‐
93              ets of type SOCK_SEQPACKET).
94
95       MSG_MORE (since Linux 2.4.4)
96              The  caller  has  more data to send.  This flag is used with TCP
97              sockets to obtain the same effect as the TCP_CORK socket  option
98              (see tcp(7)), with the difference that this flag can be set on a
99              per-call basis.
100
101              Since Linux 2.6, this flag is also supported  for  UDP  sockets,
102              and  informs the kernel to package all of the data sent in calls
103              with this flag set into a single datagram which  is  transmitted
104              only  when  a call is performed that does not specify this flag.
105              (See also the UDP_CORK socket option described in udp(7).)
106
107       MSG_NOSIGNAL (since Linux 2.2)
108              Don't generate a SIGPIPE signal if the peer on a stream-oriented
109              socket  has  closed  the  connection.   The EPIPE error is still
110              returned.  This provides similar behavior to using  sigaction(2)
111              to  ignore SIGPIPE, but, whereas MSG_NOSIGNAL is a per-call fea‐
112              ture, ignoring SIGPIPE sets a process attribute that affects all
113              threads in the process.
114
115       MSG_OOB
116              Sends  out-of-band  data  on  sockets  that  support this notion
117              (e.g., of type SOCK_STREAM); the underlying protocol  must  also
118              support out-of-band data.
119
120   sendmsg()
121       The definition of the msghdr structure employed by sendmsg() is as fol‐
122       lows:
123
124           struct msghdr {
125               void         *msg_name;       /* optional address */
126               socklen_t     msg_namelen;    /* size of address */
127               struct iovec *msg_iov;        /* scatter/gather array */
128               size_t        msg_iovlen;     /* # elements in msg_iov */
129               void         *msg_control;    /* ancillary data, see below */
130               size_t        msg_controllen; /* ancillary data buffer len */
131               int           msg_flags;      /* flags (unused) */
132           };
133
134       The msg_name field is used on an unconnected socket to specify the tar‐
135       get  address  for  a  datagram.   It  points to a buffer containing the
136       address; the msg_namelen field  should  be  set  to  the  size  of  the
137       address.   For  a connected socket, these fields should be specified as
138       NULL and 0, respectively.
139
140       The msg_iov and msg_iovlen fields specify scatter-gather locations,  as
141       for writev(2).
142
143       You  may  send  control  information using the msg_control and msg_con‐
144       trollen members.  The maximum control  buffer  length  the  kernel  can
145       process  is  limited per socket by the value in /proc/sys/net/core/opt‐
146       mem_max; see socket(7).
147
148       The msg_flags field is ignored.
149

RETURN VALUE

151       On success, these calls return the number of bytes sent.  On error,  -1
152       is returned, and errno is set appropriately.
153

ERRORS

155       These  are  some  standard errors generated by the socket layer.  Addi‐
156       tional errors may be generated and returned from the underlying  proto‐
157       col modules; see their respective manual pages.
158
159       EACCES (For  UNIX  domain  sockets,  which  are identified by pathname)
160              Write permission is denied on the destination  socket  file,  or
161              search  permission is denied for one of the directories the path
162              prefix.  (See path_resolution(7).)
163
164              (For UDP sockets)  An  attempt  was  made  to  send  to  a  net‐
165              work/broadcast address as though it was a unicast address.
166
167       EAGAIN or EWOULDBLOCK
168              The  socket  is  marked  nonblocking and the requested operation
169              would block.  POSIX.1-2001 allows either error  to  be  returned
170              for  this case, and does not require these constants to have the
171              same value, so a portable application should check for both pos‐
172              sibilities.
173
174       EAGAIN (Internet  domain  datagram  sockets)  The socket referred to by
175              sockfd had not previously been bound to  an  address  and,  upon
176              attempting  to  bind  it to an ephemeral port, it was determined
177              that all port numbers in the ephemeral port range are  currently
178              in        use.         See        the        discussion       of
179              /proc/sys/net/ipv4/ip_local_port_range in ip(7).
180
181       EALREADY
182              Another Fast Open is in progress.
183
184       EBADF  sockfd is not a valid open file descriptor.
185
186       ECONNRESET
187              Connection reset by peer.
188
189       EDESTADDRREQ
190              The socket is not connection-mode, and no peer address is set.
191
192       EFAULT An invalid user space address was specified for an argument.
193
194       EINTR  A signal occurred before any  data  was  transmitted;  see  sig‐
195              nal(7).
196
197       EINVAL Invalid argument passed.
198
199       EISCONN
200              The connection-mode socket was connected already but a recipient
201              was specified.  (Now either  this  error  is  returned,  or  the
202              recipient specification is ignored.)
203
204       EMSGSIZE
205              The  socket  type  requires that message be sent atomically, and
206              the size of the message to be sent made this impossible.
207
208       ENOBUFS
209              The output queue for a network interface was full.  This  gener‐
210              ally  indicates  that the interface has stopped sending, but may
211              be caused by transient congestion.   (Normally,  this  does  not
212              occur in Linux.  Packets are just silently dropped when a device
213              queue overflows.)
214
215       ENOMEM No memory available.
216
217       ENOTCONN
218              The socket is not connected, and no target has been given.
219
220       ENOTSOCK
221              The file descriptor sockfd does not refer to a socket.
222
223       EOPNOTSUPP
224              Some bit in the flags argument is inappropriate for  the  socket
225              type.
226
227       EPIPE  The  local  end  has  been  shut  down  on a connection oriented
228              socket.  In this case, the process will also receive  a  SIGPIPE
229              unless MSG_NOSIGNAL is set.
230

CONFORMING TO

232       4.4BSD, SVr4, POSIX.1-2001.  These interfaces first appeared in 4.2BSD.
233
234       POSIX.1-2001   describes   only   the   MSG_OOB   and   MSG_EOR  flags.
235       POSIX.1-2008 adds a specification  of  MSG_NOSIGNAL.   The  MSG_CONFIRM
236       flag is a Linux extension.
237

NOTES

239       According  to  POSIX.1-2001,  the  msg_controllen  field  of the msghdr
240       structure should be typed as socklen_t, but glibc currently types it as
241       size_t.
242
243       See sendmmsg(2) for information about a Linux-specific system call that
244       can be used to transmit multiple datagrams in a single call.
245

BUGS

247       Linux may return EPIPE instead of ENOTCONN.
248

EXAMPLE

250       An example of the use of sendto() is shown in getaddrinfo(3).
251

SEE ALSO

253       fcntl(2), getsockopt(2), recv(2), select(2), sendfile(2),  sendmmsg(2),
254       shutdown(2),  socket(2),  write(2), cmsg(3), ip(7), ipv6(7), socket(7),
255       tcp(7), udp(7), unix(7)
256

COLOPHON

258       This page is part of release 4.15 of the Linux  man-pages  project.   A
259       description  of  the project, information about reporting bugs, and the
260       latest    version    of    this    page,    can     be     found     at
261       https://www.kernel.org/doc/man-pages/.
262
263
264
265Linux                             2017-09-15                           SEND(2)
Impressum