1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" SPDX-License-Identifier: BSD-4-Clause-UC
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
12 .TH SEND 2 2022-09-09 "Linux man-pages (unreleased)"
14 send, sendto, sendmsg \- send a message on a socket
17 .RI ( libc ", " \-lc )
20 .B #include <sys/socket.h>
22 .BI "ssize_t send(int " sockfd ", const void *" buf ", size_t " len \
24 .BI "ssize_t sendto(int " sockfd ", const void *" buf ", size_t " len \
26 .BI " const struct sockaddr *" dest_addr ", socklen_t " addrlen );
27 .BI "ssize_t sendmsg(int " sockfd ", const struct msghdr *" msg \
36 are used to transmit a message to another socket.
40 call may be used only when the socket is in a
42 state (so that the intended recipient is known).
43 The only difference between
55 Also, the following call
59 send(sockfd, buf, len, flags);
67 sendto(sockfd, buf, len, flags, NULL, 0);
73 is the file descriptor of the sending socket.
77 is used on a connection-mode
84 are ignored (and the error
86 may be returned when they are
87 not NULL and 0), and the error
89 is returned when the socket was not actually connected.
90 Otherwise, the address of the target is given by
97 the address of the target is given by
107 the message is found in
113 the message is pointed to by the elements of the array
117 call also allows sending ancillary data (also known as control information).
119 If the message is too long to pass atomically through the
120 underlying protocol, the error
122 is returned, and the message is not transmitted.
124 No indication of failure to deliver is implicit in a
126 Locally detected errors are indicated by a return value of \-1.
128 When the message does not fit into the send buffer of the socket,
130 normally blocks, unless the socket has been placed in nonblocking I/O
132 In nonblocking mode it would fail with the error
139 call may be used to determine when it is possible to send more data.
140 .SS The flags argument
143 argument is the bitwise OR
144 of zero or more of the following flags.
145 .\" FIXME . ? document MSG_PROXY (which went away in 2.3.15)
147 .BR MSG_CONFIRM " (since Linux 2.3.15)"
148 Tell the link layer that forward progress happened: you got a successful
149 reply from the other side.
150 If the link layer doesn't get this
151 it will regularly reprobe the neighbor (e.g., via a unicast ARP).
156 sockets and currently implemented only for IPv4 and IPv6.
162 Don't use a gateway to send out the packet, send to hosts only on
163 directly connected networks.
164 This is usually used only
165 by diagnostic or routing programs.
166 This is defined only for protocol
167 families that route; packet sockets don't.
169 .BR MSG_DONTWAIT " (since Linux 2.2)"
170 Enables nonblocking operation; if the operation would block,
175 This provides similar behavior to setting the
180 operation), but differs in that
182 is a per-call option, whereas
184 is a setting on the open file description (see
186 which will affect all threads in the calling process
187 and as well as other processes that hold file descriptors
188 referring to the same open file description.
190 .BR MSG_EOR " (since Linux 2.2)"
191 Terminates a record (when this notion is supported, as for sockets of type
192 .BR SOCK_SEQPACKET ).
194 .BR MSG_MORE " (since Linux 2.4.4)"
195 The caller has more data to send.
196 This flag is used with TCP sockets to obtain the same effect
201 with the difference that this flag can be set on a per-call basis.
203 Since Linux 2.6, this flag is also supported for UDP sockets, and informs
204 the kernel to package all of the data sent in calls with this flag set
205 into a single datagram which is transmitted only when a call is performed
206 that does not specify this flag.
209 socket option described in
212 .BR MSG_NOSIGNAL " (since Linux 2.2)"
215 signal if the peer on a stream-oriented socket has closed the connection.
218 error is still returned.
219 This provides similar behavior to using
225 is a per-call feature,
228 sets a process attribute that affects all threads in the process.
233 data on sockets that support this notion (e.g., of type
235 the underlying protocol must also support
239 .BR MSG_FASTOPEN " (since Linux 3.7)"
240 Attempts TCP Fast Open (RFC7413) and sends data in the SYN like a
245 by performing an implicit
248 It blocks until the data is buffered and the handshake has completed.
249 For a non-blocking socket,
250 it returns the number of bytes buffered and sent in the SYN packet.
251 If the cookie is not available locally,
254 and sends a SYN with a Fast Open cookie request automatically.
255 The caller needs to write the data again when the socket is connected.
261 if the handshake fails.
262 This flag requires enabling TCP Fast Open client support on sysctl
263 .IR net.ipv4.tcp_fastopen .
266 .B TCP_FASTOPEN_CONNECT
269 for an alternative approach.
271 The definition of the
273 structure employed by
280 void *msg_name; /* Optional address */
281 socklen_t msg_namelen; /* Size of address */
282 struct iovec *msg_iov; /* Scatter/gather array */
283 size_t msg_iovlen; /* # elements in msg_iov */
284 void *msg_control; /* Ancillary data, see below */
285 size_t msg_controllen; /* Ancillary data buffer len */
286 int msg_flags; /* Flags (unused) */
293 field is used on an unconnected socket to specify the target
294 address for a datagram.
295 It points to a buffer containing the address; the
297 field should be set to the size of the address.
298 For a connected socket, these fields should be specified as NULL and 0,
305 fields specify scatter-gather locations, as for
308 You may send control information (ancillary data) using the
313 The maximum control buffer length the kernel can process is limited
314 per socket by the value in
315 .IR /proc/sys/net/core/optmem_max ;
318 For further information on the use of ancillary data in various
327 .\" Still to be documented:
328 .\" Send file descriptors and user credentials using the
329 .\" msg_control* fields.
331 On success, these calls return the number of bytes sent.
332 On error, \-1 is returned, and
334 is set to indicate the error.
336 These are some standard errors generated by the socket layer.
338 may be generated and returned from the underlying protocol modules;
339 see their respective manual pages.
342 (For UNIX domain sockets, which are identified by pathname)
343 Write permission is denied on the destination socket file,
344 or search permission is denied for one of the directories
347 .BR path_resolution (7).)
349 (For UDP sockets) An attempt was made to send to a
350 network/broadcast address as though it was a unicast address.
352 .BR EAGAIN " or " EWOULDBLOCK
353 .\" Actually EAGAIN on Linux
354 The socket is marked nonblocking and the requested operation
356 POSIX.1-2001 allows either error to be returned for this case,
357 and does not require these constants to have the same value,
358 so a portable application should check for both possibilities.
361 (Internet domain datagram sockets)
362 The socket referred to by
364 had not previously been bound to an address and,
365 upon attempting to bind it to an ephemeral port,
366 it was determined that all port numbers in the ephemeral port range
367 are currently in use.
368 See the discussion of
369 .I /proc/sys/net/ipv4/ip_local_port_range
374 Another Fast Open is in progress.
378 is not a valid open file descriptor.
381 Connection reset by peer.
384 The socket is not connection-mode, and no peer address is set.
387 An invalid user space address was specified for an argument.
390 A signal occurred before any data was transmitted; see
394 Invalid argument passed.
397 The connection-mode socket was connected already but a
398 recipient was specified.
399 (Now either this error is returned, or the recipient specification
404 .\" (e.g., SOCK_DGRAM )
405 requires that message be sent atomically, and the size
406 of the message to be sent made this impossible.
409 The output queue for a network interface was full.
410 This generally indicates that the interface has stopped sending,
411 but may be caused by transient congestion.
412 (Normally, this does not occur in Linux.
413 Packets are just silently dropped
414 when a device queue overflows.)
420 The socket is not connected, and no target has been given.
425 does not refer to a socket.
430 argument is inappropriate for the socket type.
433 The local end has been shut down on a connection oriented socket.
434 In this case, the process
441 4.4BSD, SVr4, POSIX.1-2001.
442 These interfaces first appeared in 4.2BSD.
444 POSIX.1-2001 describes only the
449 POSIX.1-2008 adds a specification of
453 flag is a Linux extension.
455 According to POSIX.1-2001, the
459 structure should be typed as
463 field should be typed as
465 but glibc currently types both as
467 .\" glibc bug for msg_controllen raised 12 Mar 2006
468 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=2448
469 .\" The problem is an underlying kernel issue: the size of the
470 .\" __kernel_size_t type used to type these fields varies
471 .\" across architectures, but socklen_t is always 32 bits,
472 .\" as (at least with GCC) is int.
476 for information about a Linux-specific system call
477 that can be used to transmit multiple datagrams in a single call.
484 An example of the use of