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/socket.h>
10
11       ssize_t send(int sockfd, const void *buf, size_t len, int flags);
12       ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
13                      const struct sockaddr *dest_addr, socklen_t addrlen);
14       ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
15

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

CONFORMING TO

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

NOTES

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

BUGS

246       Linux may return EPIPE instead of ENOTCONN.
247

EXAMPLES

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

SEE ALSO

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

COLOPHON

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