1send(2)                       System Calls Manual                      send(2)
2
3
4

NAME

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

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <sys/socket.h>
13
14       ssize_t send(int sockfd, const void buf[.len], size_t len, int flags);
15       ssize_t sendto(int sockfd, const void buf[.len], size_t len, int flags,
16                      const struct sockaddr *dest_addr, socklen_t addrlen);
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, EA‐
83              GAIN or EWOULDBLOCK is returned.  This provides similar behavior
84              to  setting the O_NONBLOCK flag (via the fcntl(2) F_SETFL opera‐
85              tion), but differs in that MSG_DONTWAIT is  a  per-call  option,
86              whereas  O_NONBLOCK  is  a  setting on the open file description
87              (see open(2)), which will affect  all  threads  in  the  calling
88              process  and  as well as other processes that hold file descrip‐
89              tors 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 re‐
110              turned.  This provides similar behavior to using sigaction(2) to
111              ignore SIGPIPE, but, whereas MSG_NOSIGNAL is a per-call feature,
112              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       MSG_FASTOPEN (since Linux 3.7)
121              Attempts  TCP Fast Open (RFC7413) and sends data in the SYN like
122              a combination of connect(2) and write(2), by performing  an  im‐
123              plicit  connect(2)  operation.   It  blocks  until  the  data is
124              buffered and the handshake has completed.   For  a  non-blocking
125              socket,  it returns the number of bytes buffered and sent in the
126              SYN packet.  If the cookie is not available locally, it  returns
127              EINPROGRESS, and sends a SYN with a Fast Open cookie request au‐
128              tomatically.  The caller needs to write the data again when  the
129              socket  is connected.  On errors, it sets the same errno as con‐
130              nect(2) if the handshake fails.  This flag requires enabling TCP
131              Fast Open client support on sysctl net.ipv4.tcp_fastopen.
132
133              Refer to TCP_FASTOPEN_CONNECT socket option in tcp(7) for an al‐
134              ternative approach.
135
136   sendmsg()
137       The definition of the msghdr structure employed by sendmsg() is as fol‐
138       lows:
139
140           struct msghdr {
141               void         *msg_name;       /* Optional address */
142               socklen_t     msg_namelen;    /* Size of address */
143               struct iovec *msg_iov;        /* Scatter/gather array */
144               size_t        msg_iovlen;     /* # elements in msg_iov */
145               void         *msg_control;    /* Ancillary data, see below */
146               size_t        msg_controllen; /* Ancillary data buffer len */
147               int           msg_flags;      /* Flags (unused) */
148           };
149
150       The msg_name field is used on an unconnected socket to specify the tar‐
151       get address for a datagram.  It points to a buffer containing  the  ad‐
152       dress;  the msg_namelen field should be set to the size of the address.
153       For a connected socket, these fields should be specified as NULL and 0,
154       respectively.
155
156       The  msg_iov and msg_iovlen fields specify scatter-gather locations, as
157       for writev(2).
158
159       You may send control information (ancillary data) using the msg_control
160       and msg_controllen members.  The maximum control buffer length the ker‐
161       nel  can  process   is   limited   per   socket   by   the   value   in
162       /proc/sys/net/core/optmem_max;  see socket(7).  For further information
163       on the use of ancillary data in various socket domains, see unix(7) and
164       ip(7).
165
166       The msg_flags field is ignored.
167

RETURN VALUE

169       On  success, these calls return the number of bytes sent.  On error, -1
170       is returned, and errno is set to indicate the error.
171

ERRORS

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

VERSIONS

250       According to POSIX.1-2001,  the  msg_controllen  field  of  the  msghdr
251       structure should be typed as socklen_t, and the msg_iovlen field should
252       be typed as int, but glibc currently types both as size_t.
253

STANDARDS

255       POSIX.1-2008.
256
257       MSG_CONFIRM is a Linux extension.
258

HISTORY

260       4.4BSD, SVr4, POSIX.1-2001.  (first appeared in 4.2BSD).
261
262       POSIX.1-2001  describes   only   the   MSG_OOB   and   MSG_EOR   flags.
263       POSIX.1-2008 adds a specification of MSG_NOSIGNAL.
264

NOTES

266       See sendmmsg(2) for information about a Linux-specific system call that
267       can be used to transmit multiple datagrams in a single call.
268

BUGS

270       Linux may return EPIPE instead of ENOTCONN.
271

EXAMPLES

273       An example of the use of sendto() is shown in getaddrinfo(3).
274

SEE ALSO

276       fcntl(2), getsockopt(2), recv(2), select(2), sendfile(2),  sendmmsg(2),
277       shutdown(2),  socket(2),  write(2), cmsg(3), ip(7), ipv6(7), socket(7),
278       tcp(7), udp(7), unix(7)
279
280
281
282Linux man-pages 6.05              2023-03-30                           send(2)
Impressum