]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/send.2
man*/: srcfix (Use .P instead of .PP or .LP)
[thirdparty/man-pages.git] / man2 / send.2
CommitLineData
fea681da
MK
1.\" Copyright (c) 1983, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
47009d5e 4.\" SPDX-License-Identifier: BSD-4-Clause-UC
fea681da
MK
5.\"
6.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
7.\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
8.\" Modified Oct 1998 by Andi Kleen
9.\" Modified Oct 2003 by aeb
10.\" Modified 2004-07-01 by mtk
11.\"
4c1c5274 12.TH send 2 (date) "Linux man-pages (unreleased)"
fea681da 13.SH NAME
d3018c3c 14send, sendto, sendmsg \- send a message on a socket
6021c9a2
AC
15.SH LIBRARY
16Standard C library
8fc3b2cf 17.RI ( libc ", " \-lc )
fea681da 18.SH SYNOPSIS
521bf584 19.nf
fea681da 20.B #include <sys/socket.h>
c6d039a3 21.P
c64cd13e 22.BI "ssize_t send(int " sockfd ", const void " buf [. len "], size_t " len \
521bf584 23", int " flags );
c64cd13e 24.BI "ssize_t sendto(int " sockfd ", const void " buf [. len "], size_t " len \
521bf584 25", int " flags ,
6926dbf5 26.BI " const struct sockaddr *" dest_addr ", socklen_t " addrlen );
c4e7b714 27.BI "ssize_t sendmsg(int " sockfd ", const struct msghdr *" msg \
521bf584
MK
28", int " flags );
29.fi
fea681da
MK
30.SH DESCRIPTION
31The system calls
e511ffb6
MK
32.BR send (),
33.BR sendto (),
fea681da 34and
e511ffb6 35.BR sendmsg ()
fea681da 36are used to transmit a message to another socket.
c6d039a3 37.P
fea681da 38The
e511ffb6 39.BR send ()
c13182ef 40call may be used only when the socket is in a
fea681da
MK
41.I connected
42state (so that the intended recipient is known).
43The only difference between
e511ffb6 44.BR send ()
fea681da 45and
0bfa087b 46.BR write (2)
fea681da
MK
47is the presence of
48.IR flags .
3701130a 49With a zero
fea681da 50.I flags
c4bb193f 51argument,
e511ffb6 52.BR send ()
fea681da 53is equivalent to
0bfa087b 54.BR write (2).
167441ee 55Also, the following call
c6d039a3 56.P
1ae6b2c7
AC
57.in +4n
58.EX
59send(sockfd, buf, len, flags);
60.EE
61.in
c6d039a3 62.P
fea681da 63is equivalent to
c6d039a3 64.P
1ae6b2c7
AC
65.in +4n
66.EX
67sendto(sockfd, buf, len, flags, NULL, 0);
68.EE
69.in
c6d039a3 70.P
c4bb193f 71The argument
b56905c6 72.I sockfd
fea681da 73is the file descriptor of the sending socket.
c6d039a3 74.P
fea681da 75If
e511ffb6 76.BR sendto ()
097585ed
MK
77is used on a connection-mode
78.RB ( SOCK_STREAM ,
79.BR SOCK_SEQPACKET )
c4bb193f 80socket, the arguments
6926dbf5 81.I dest_addr
fea681da 82and
6926dbf5 83.I addrlen
1274071a
MK
84are ignored (and the error
85.B EISCONN
86may be returned when they are
097585ed
MK
87not NULL and 0), and the error
88.B ENOTCONN
89is returned when the socket was not actually connected.
c13182ef 90Otherwise, the address of the target is given by
6926dbf5 91.I dest_addr
c13182ef 92with
6926dbf5 93.I addrlen
fea681da
MK
94specifying its size.
95For
e511ffb6 96.BR sendmsg (),
fea681da
MK
97the address of the target is given by
98.IR msg.msg_name ,
99with
100.I msg.msg_namelen
101specifying its size.
c6d039a3 102.P
fea681da 103For
e511ffb6 104.BR send ()
fea681da 105and
e511ffb6 106.BR sendto (),
fea681da
MK
107the message is found in
108.I buf
109and has length
110.IR len .
111For
e511ffb6 112.BR sendmsg (),
fea681da
MK
113the message is pointed to by the elements of the array
114.IR msg.msg_iov .
115The
e511ffb6 116.BR sendmsg ()
fea681da 117call also allows sending ancillary data (also known as control information).
c6d039a3 118.P
fea681da
MK
119If the message is too long to pass atomically through the
120underlying protocol, the error
121.B EMSGSIZE
122is returned, and the message is not transmitted.
c6d039a3 123.P
fea681da 124No indication of failure to deliver is implicit in a
e511ffb6 125.BR send ().
fea681da 126Locally detected errors are indicated by a return value of \-1.
c6d039a3 127.P
fea681da 128When the message does not fit into the send buffer of the socket,
e511ffb6 129.BR send ()
ff40dbb3 130normally blocks, unless the socket has been placed in nonblocking I/O
c13182ef 131mode.
ff40dbb3 132In nonblocking mode it would fail with the error
fea681da 133.B EAGAIN
86426e0b
MK
134or
135.B EWOULDBLOCK
fea681da
MK
136in this case.
137The
138.BR select (2)
139call may be used to determine when it is possible to send more data.
85b1cf3c 140.SS The flags argument
fea681da
MK
141The
142.I flags
c4bb193f 143argument is the bitwise OR
fea681da 144of zero or more of the following flags.
b324e17d 145.\" FIXME . ? document MSG_PROXY (which went away in Linux 2.3.15)
fea681da 146.TP
31c1f2b0 147.BR MSG_CONFIRM " (since Linux 2.3.15)"
a28ed21c 148Tell the link layer that forward progress happened: you got a successful
c13182ef
MK
149reply from the other side.
150If the link layer doesn't get this
75b94dc3 151it will regularly reprobe the neighbor (e.g., via a unicast ARP).
68246229 152Valid only on
a28ed21c
MK
153.B SOCK_DGRAM
154and
155.B SOCK_RAW
33a0ccb2 156sockets and currently implemented only for IPv4 and IPv6.
c13182ef 157See
a28ed21c
MK
158.BR arp (7)
159for details.
fea681da
MK
160.TP
161.B MSG_DONTROUTE
33a0ccb2 162Don't use a gateway to send out the packet, send to hosts only on
c13182ef
MK
163directly connected networks.
164This is usually used only
165by diagnostic or routing programs.
33a0ccb2 166This is defined only for protocol
fea681da
MK
167families that route; packet sockets don't.
168.TP
6eb4bccc 169.BR MSG_DONTWAIT " (since Linux 2.2)"
ff40dbb3 170Enables nonblocking operation; if the operation would block,
fea681da 171.B EAGAIN
86426e0b
MK
172or
173.B EWOULDBLOCK
630b4cac
MK
174is returned.
175This provides similar behavior to setting the
fea681da 176.B O_NONBLOCK
630b4cac
MK
177flag (via the
178.BR fcntl (2)
fea681da 179.B F_SETFL
630b4cac
MK
180operation), but differs in that
181.B MSG_DONTWAIT
182is a per-call option, whereas
183.B O_NONBLOCK
184is a setting on the open file description (see
185.BR open (2)),
186which will affect all threads in the calling process
187and as well as other processes that hold file descriptors
188referring to the same open file description.
fea681da 189.TP
6eb4bccc 190.BR MSG_EOR " (since Linux 2.2)"
a28ed21c
MK
191Terminates a record (when this notion is supported, as for sockets of type
192.BR SOCK_SEQPACKET ).
fea681da 193.TP
31c1f2b0 194.BR MSG_MORE " (since Linux 2.4.4)"
fea681da
MK
195The caller has more data to send.
196This flag is used with TCP sockets to obtain the same effect
2f0af33b
MK
197as the
198.B TCP_CORK
199socket option (see
fea681da
MK
200.BR tcp (7)),
201with the difference that this flag can be set on a per-call basis.
efeece04 202.IP
fea681da
MK
203Since Linux 2.6, this flag is also supported for UDP sockets, and informs
204the kernel to package all of the data sent in calls with this flag set
33a0ccb2 205into a single datagram which is transmitted only when a call is performed
c13182ef
MK
206that does not specify this flag.
207(See also the
208.B UDP_CORK
209socket option described in
145ff024 210.BR udp (7).)
a28ed21c 211.TP
6eb4bccc 212.BR MSG_NOSIGNAL " (since Linux 2.2)"
19aecb7e 213Don't generate a
c13182ef 214.B SIGPIPE
19aecb7e 215signal if the peer on a stream-oriented socket has closed the connection.
c13182ef 216The
a28ed21c
MK
217.B EPIPE
218error is still returned.
d3b019c1
MK
219This provides similar behavior to using
220.BR sigaction (2)
221to ignore
222.BR SIGPIPE ,
223but, whereas
224.B MSG_NOSIGNAL
225is a per-call feature,
226ignoring
227.B SIGPIPE
228sets a process attribute that affects all threads in the process.
a28ed21c
MK
229.TP
230.B MSG_OOB
231Sends
232.I out-of-band
75b94dc3 233data on sockets that support this notion (e.g., of type
a28ed21c
MK
234.BR SOCK_STREAM );
235the underlying protocol must also support
236.I out-of-band
237data.
dda0c305
WW
238.TP
239.BR MSG_FASTOPEN " (since Linux 3.7)"
240Attempts TCP Fast Open (RFC7413) and sends data in the SYN like a
241combination of
242.BR connect (2)
243and
244.BR write (2),
245by performing an implicit
246.BR connect (2)
247operation.
248It blocks until the data is buffered and the handshake has completed.
249For a non-blocking socket,
250it returns the number of bytes buffered and sent in the SYN packet.
251If the cookie is not available locally,
252it returns
253.BR EINPROGRESS ,
254and sends a SYN with a Fast Open cookie request automatically.
255The caller needs to write the data again when the socket is connected.
256On errors,
257it sets the same
258.I errno
259as
260.BR connect (2)
261if the handshake fails.
262This flag requires enabling TCP Fast Open client support on sysctl
263.IR net.ipv4.tcp_fastopen .
264.IP
265Refer to
266.B TCP_FASTOPEN_CONNECT
267socket option in
268.BR tcp (7)
269for an alternative approach.
85b1cf3c 270.SS sendmsg()
fea681da
MK
271The definition of the
272.I msghdr
7784c37d
MK
273structure employed by
274.BR sendmsg ()
275is as follows:
c6d039a3 276.P
a08ea57c 277.in +4n
e646a1ba 278.EX
fea681da 279struct msghdr {
ba9fc3e1
MK
280 void *msg_name; /* Optional address */
281 socklen_t msg_namelen; /* Size of address */
282 struct iovec *msg_iov; /* Scatter/gather array */
ea4adf4b 283 size_t msg_iovlen; /* # elements in msg_iov */
ba9fc3e1
MK
284 void *msg_control; /* Ancillary data, see below */
285 size_t msg_controllen; /* Ancillary data buffer len */
286 int msg_flags; /* Flags (unused) */
fea681da 287};
b8302363 288.EE
a08ea57c 289.in
c6d039a3 290.P
7784c37d
MK
291The
292.I msg_name
293field is used on an unconnected socket to specify the target
294address for a datagram.
295It points to a buffer containing the address; the
d1c05d0b 296.I msg_namelen
7784c37d
MK
297field should be set to the size of the address.
298For a connected socket, these fields should be specified as NULL and 0,
299respectively.
c6d039a3 300.P
7784c37d
MK
301The
302.I msg_iov
303and
304.I msg_iovlen
305fields specify scatter-gather locations, as for
306.BR writev (2).
c6d039a3 307.P
0f849717 308You may send control information (ancillary data) using the
c13182ef
MK
309.I msg_control
310and
311.I msg_controllen
312members.
313The maximum control buffer length the kernel can process is limited
5a2ff571
MK
314per socket by the value in
315.IR /proc/sys/net/core/optmem_max ;
316see
fea681da 317.BR socket (7).
2650e827
MK
318For further information on the use of ancillary data in various
319socket domains, see
320.BR unix (7)
321and
322.BR ip (7).
c6d039a3 323.P
7784c37d
MK
324The
325.I msg_flags
326field is ignored.
fea681da
MK
327.\" Still to be documented:
328.\" Send file descriptors and user credentials using the
329.\" msg_control* fields.
47297adb 330.SH RETURN VALUE
4eec1609 331On success, these calls return the number of bytes sent.
cca657e2
MK
332On error, \-1 is returned, and
333.I errno
f6a4078b 334is set to indicate the error.
fea681da 335.SH ERRORS
c13182ef
MK
336These are some standard errors generated by the socket layer.
337Additional errors
338may be generated and returned from the underlying protocol modules;
339see their respective manual pages.
fea681da
MK
340.TP
341.B EACCES
008f1ecc 342(For UNIX domain sockets, which are identified by pathname)
fea681da
MK
343Write permission is denied on the destination socket file,
344or search permission is denied for one of the directories
c13182ef
MK
345the path prefix.
346(See
ad7cc990 347.BR path_resolution (7).)
bdd915e2 348.IP
4c49bf90
SP
349(For UDP sockets) An attempt was made to send to a
350network/broadcast address as though it was a unicast address.
fea681da
MK
351.TP
352.BR EAGAIN " or " EWOULDBLOCK
86426e0b 353.\" Actually EAGAIN on Linux
ff40dbb3 354The socket is marked nonblocking and the requested operation
fea681da 355would block.
86426e0b
MK
356POSIX.1-2001 allows either error to be returned for this case,
357and does not require these constants to have the same value,
358so a portable application should check for both possibilities.
fea681da 359.TP
16f7b04c
MK
360.B EAGAIN
361(Internet domain datagram sockets)
362The socket referred to by
363.I sockfd
364had not previously been bound to an address and,
365upon attempting to bind it to an ephemeral port,
366it was determined that all port numbers in the ephemeral port range
367are currently in use.
368See the discussion of
369.I /proc/sys/net/ipv4/ip_local_port_range
370in
371.BR ip (7).
372.TP
b61f53a4
GVS
373.B EALREADY
374Another Fast Open is in progress.
375.TP
fea681da 376.B EBADF
d9cb0d7d
MK
377.I sockfd
378is not a valid open file descriptor.
fea681da
MK
379.TP
380.B ECONNRESET
381Connection reset by peer.
382.TP
383.B EDESTADDRREQ
384The socket is not connection-mode, and no peer address is set.
385.TP
386.B EFAULT
c4bb193f 387An invalid user space address was specified for an argument.
fea681da
MK
388.TP
389.B EINTR
01538d0d
MK
390A signal occurred before any data was transmitted; see
391.BR signal (7).
fea681da
MK
392.TP
393.B EINVAL
394Invalid argument passed.
395.TP
396.B EISCONN
397The connection-mode socket was connected already but a
398recipient was specified.
399(Now either this error is returned, or the recipient specification
400is ignored.)
401.TP
402.B EMSGSIZE
403The socket type
404.\" (e.g., SOCK_DGRAM )
405requires that message be sent atomically, and the size
406of the message to be sent made this impossible.
407.TP
408.B ENOBUFS
409The output queue for a network interface was full.
410This generally indicates that the interface has stopped sending,
411but may be caused by transient congestion.
c13182ef
MK
412(Normally, this does not occur in Linux.
413Packets are just silently dropped
fea681da
MK
414when a device queue overflows.)
415.TP
416.B ENOMEM
417No memory available.
418.TP
419.B ENOTCONN
420The socket is not connected, and no target has been given.
421.TP
422.B ENOTSOCK
deedfd97 423The file descriptor
3e4088f4 424.I sockfd
deedfd97 425does not refer to a socket.
fea681da
MK
426.TP
427.B EOPNOTSUPP
428Some bit in the
429.I flags
430argument is inappropriate for the socket type.
431.TP
432.B EPIPE
433The local end has been shut down on a connection oriented socket.
1e9b8f7f 434In this case, the process
c13182ef
MK
435will also receive a
436.B SIGPIPE
437unless
438.B MSG_NOSIGNAL
fea681da 439is set.
4131356c 440.SH VERSIONS
ea4adf4b
MK
441According to POSIX.1-2001, the
442.I msg_controllen
443field of the
444.I msghdr
445structure should be typed as
446.IR socklen_t ,
71d88927
AR
447and the
448.I msg_iovlen
449field should be typed as
450.IR int ,
451but glibc currently types both as
ea4adf4b 452.IR size_t .
71d88927 453.\" glibc bug for msg_controllen raised 12 Mar 2006
ea4adf4b 454.\" http://sourceware.org/bugzilla/show_bug.cgi?id=2448
1c511da2 455.\" The problem is an underlying kernel issue: the size of the
71d88927
AR
456.\" __kernel_size_t type used to type these fields varies
457.\" across architectures, but socklen_t is always 32 bits,
458.\" as (at least with GCC) is int.
4131356c
AC
459.SH STANDARDS
460POSIX.1-2008.
c6d039a3 461.P
4131356c
AC
462.B MSG_CONFIRM
463is a Linux extension.
464.SH HISTORY
4654.4BSD, SVr4, POSIX.1-2001.
466(first appeared in 4.2BSD).
c6d039a3 467.P
4131356c
AC
468POSIX.1-2001 describes only the
469.B MSG_OOB
470and
471.B MSG_EOR
472flags.
473POSIX.1-2008 adds a specification of
474.BR MSG_NOSIGNAL .
475.SH NOTES
53aeb9c2 476See
6fdbc779 477.BR sendmmsg (2)
53aeb9c2
MK
478for information about a Linux-specific system call
479that can be used to transmit multiple datagrams in a single call.
fea681da 480.SH BUGS
1274071a
MK
481Linux may return
482.B EPIPE
2f0af33b
MK
483instead of
484.BR ENOTCONN .
a14af333 485.SH EXAMPLES
1c27853f
MK
486An example of the use of
487.BR sendto ()
488is shown in
489.BR getaddrinfo (3).
47297adb 490.SH SEE ALSO
fea681da
MK
491.BR fcntl (2),
492.BR getsockopt (2),
493.BR recv (2),
494.BR select (2),
495.BR sendfile (2),
53aeb9c2 496.BR sendmmsg (2),
85a3dc20 497.BR shutdown (2),
fea681da
MK
498.BR socket (2),
499.BR write (2),
1df419f2 500.BR cmsg (3),
fea681da 501.BR ip (7),
b9cde461 502.BR ipv6 (7),
fea681da
MK
503.BR socket (7),
504.BR tcp (7),
b9cde461 505.BR udp (7),
b9cde461 506.BR unix (7)