OSDN Git Service

(split) LDP: Update original to v3.37.
[linuxjm/LDP_man-pages.git] / original / man2 / send.2
1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
33 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
34 .\" Modified Oct 1998 by Andi Kleen
35 .\" Modified Oct 2003 by aeb
36 .\" Modified 2004-07-01 by mtk
37 .\"
38 .TH SEND 2 2012-02-27 "Linux" "Linux Programmer's Manual"
39 .SH NAME
40 send, sendto, sendmsg \- send a message on a socket
41 .SH SYNOPSIS
42 .nf
43 .B #include <sys/types.h>
44 .B #include <sys/socket.h>
45 .sp
46 .BI "ssize_t send(int " sockfd ", const void *" buf ", size_t " len \
47 ", int " flags );
48
49 .BI "ssize_t sendto(int " sockfd ", const void *" buf ", size_t " len \
50 ", int " flags ,
51 .BI "               const struct sockaddr *" dest_addr ", socklen_t " addrlen );
52
53 .BI "ssize_t sendmsg(int " sockfd ", const struct msghdr *" msg \
54 ", int " flags );
55 .fi
56 .SH DESCRIPTION
57 The system calls
58 .BR send (),
59 .BR sendto (),
60 and
61 .BR sendmsg ()
62 are used to transmit a message to another socket.
63 .PP
64 The
65 .BR send ()
66 call may be used only when the socket is in a
67 .I connected
68 state (so that the intended recipient is known).
69 The only difference between
70 .BR send ()
71 and
72 .BR write (2)
73 is the presence of
74 .IR flags .
75 With a zero
76 .I flags
77 argument,
78 .BR send ()
79 is equivalent to
80 .BR write (2).
81 Also, the following call
82
83     send(sockfd, buf, len, flags);
84
85 is equivalent to
86
87     sendto(sockfd, buf, len, flags, NULL, 0);
88 .PP
89 The argument
90 .I sockfd
91 is the file descriptor of the sending socket.
92 .PP
93 If
94 .BR sendto ()
95 is used on a connection-mode
96 .RB ( SOCK_STREAM ,
97 .BR SOCK_SEQPACKET )
98 socket, the arguments
99 .I dest_addr
100 and
101 .I addrlen
102 are ignored (and the error
103 .B EISCONN
104 may be returned when they are
105 not NULL and 0), and the error
106 .B ENOTCONN
107 is returned when the socket was not actually connected.
108 Otherwise, the address of the target is given by
109 .I dest_addr
110 with
111 .I addrlen
112 specifying its size.
113 For
114 .BR sendmsg (),
115 the address of the target is given by
116 .IR msg.msg_name ,
117 with
118 .I msg.msg_namelen
119 specifying its size.
120 .PP
121 For
122 .BR send ()
123 and
124 .BR sendto (),
125 the message is found in
126 .I buf
127 and has length
128 .IR len .
129 For
130 .BR sendmsg (),
131 the message is pointed to by the elements of the array
132 .IR msg.msg_iov .
133 The
134 .BR sendmsg ()
135 call also allows sending ancillary data (also known as control information).
136 .PP
137 If the message is too long to pass atomically through the
138 underlying protocol, the error
139 .B EMSGSIZE
140 is returned, and the message is not transmitted.
141 .PP
142 No indication of failure to deliver is implicit in a
143 .BR send ().
144 Locally detected errors are indicated by a return value of \-1.
145 .PP
146 When the message does not fit into the send buffer of the socket,
147 .BR send ()
148 normally blocks, unless the socket has been placed in nonblocking I/O
149 mode.
150 In nonblocking mode it would fail with the error
151 .B EAGAIN
152 or
153 .B EWOULDBLOCK
154 in this case.
155 The
156 .BR select (2)
157 call may be used to determine when it is possible to send more data.
158 .PP
159 The
160 .I flags
161 argument is the bitwise OR
162 of zero or more of the following flags.
163 .\" FIXME ? document MSG_PROXY (which went away in 2.3.15)
164 .TP
165 .BR MSG_CONFIRM " (Since Linux 2.3.15)"
166 Tell the link layer that forward progress happened: you got a successful
167 reply from the other side.
168 If the link layer doesn't get this
169 it will regularly reprobe the neighbor (e.g., via a unicast ARP).
170 Only valid on
171 .B SOCK_DGRAM
172 and
173 .B SOCK_RAW
174 sockets and currently only implemented for IPv4 and IPv6.
175 See
176 .BR arp (7)
177 for details.
178 .TP
179 .B MSG_DONTROUTE
180 Don't use a gateway to send out the packet, only send to hosts on
181 directly connected networks.
182 This is usually used only
183 by diagnostic or routing programs.
184 This is only defined for protocol
185 families that route; packet sockets don't.
186 .TP
187 .BR MSG_DONTWAIT " (since Linux 2.2)"
188 Enables nonblocking operation; if the operation would block,
189 .B EAGAIN
190 or
191 .B EWOULDBLOCK
192 is returned (this can also be enabled using the
193 .B O_NONBLOCK
194 flag with the
195 .B F_SETFL
196 .BR fcntl (2)).
197 .TP
198 .BR MSG_EOR " (since Linux 2.2)"
199 Terminates a record (when this notion is supported, as for sockets of type
200 .BR SOCK_SEQPACKET ).
201 .TP
202 .BR MSG_MORE " (Since Linux 2.4.4)"
203 The caller has more data to send.
204 This flag is used with TCP sockets to obtain the same effect
205 as the
206 .B TCP_CORK
207 socket option (see
208 .BR tcp (7)),
209 with the difference that this flag can be set on a per-call basis.
210
211 Since Linux 2.6, this flag is also supported for UDP sockets, and informs
212 the kernel to package all of the data sent in calls with this flag set
213 into a single datagram which is only transmitted when a call is performed
214 that does not specify this flag.
215 (See also the
216 .B UDP_CORK
217 socket option described in
218 .BR udp (7).)
219 .TP
220 .BR MSG_NOSIGNAL " (since Linux 2.2)"
221 Requests not to send
222 .B SIGPIPE
223 on errors on stream oriented sockets when the other end breaks the
224 connection.
225 The
226 .B EPIPE
227 error is still returned.
228 .TP
229 .B MSG_OOB
230 Sends
231 .I out-of-band
232 data on sockets that support this notion (e.g., of type
233 .BR SOCK_STREAM );
234 the underlying protocol must also support
235 .I out-of-band
236 data.
237 .PP
238 The definition of the
239 .I msghdr
240 structure follows.
241 See
242 .BR recv (2)
243 and below for an exact description of its fields.
244 .in +4n
245 .nf
246
247 struct msghdr {
248     void         *msg_name;       /* optional address */
249     socklen_t     msg_namelen;    /* size of address */
250     struct iovec *msg_iov;        /* scatter/gather array */
251     size_t        msg_iovlen;     /* # elements in msg_iov */
252     void         *msg_control;    /* ancillary data, see below */
253     size_t        msg_controllen; /* ancillary data buffer len */
254     int           msg_flags;      /* flags on received message */
255 };
256 .fi
257 .in
258 .PP
259 You may send control information using the
260 .I msg_control
261 and
262 .I msg_controllen
263 members.
264 The maximum control buffer length the kernel can process is limited
265 per socket by the value in
266 .IR /proc/sys/net/core/optmem_max ;
267 see
268 .BR socket (7).
269 .\" Still to be documented:
270 .\"  Send file descriptors and user credentials using the
271 .\"  msg_control* fields.
272 .\"  The flags returned in msg_flags.
273 .SH "RETURN VALUE"
274 On success, these calls return the number of characters sent.
275 On error, \-1 is returned, and
276 .I errno
277 is set appropriately.
278 .SH ERRORS
279 These are some standard errors generated by the socket layer.
280 Additional errors
281 may be generated and returned from the underlying protocol modules;
282 see their respective manual pages.
283 .TP
284 .B EACCES
285 (For UNIX domain sockets, which are identified by pathname)
286 Write permission is denied on the destination socket file,
287 or search permission is denied for one of the directories
288 the path prefix.
289 (See
290 .BR path_resolution (7).)
291 .TP
292 .BR EAGAIN " or " EWOULDBLOCK
293 .\" Actually EAGAIN on Linux
294 The socket is marked nonblocking and the requested operation
295 would block.
296 POSIX.1-2001 allows either error to be returned for this case,
297 and does not require these constants to have the same value,
298 so a portable application should check for both possibilities.
299 .TP
300 .B EBADF
301 An invalid descriptor was specified.
302 .TP
303 .B ECONNRESET
304 Connection reset by peer.
305 .TP
306 .B EDESTADDRREQ
307 The socket is not connection-mode, and no peer address is set.
308 .TP
309 .B EFAULT
310 An invalid user space address was specified for an argument.
311 .TP
312 .B EINTR
313 A signal occurred before any data was transmitted; see
314 .BR signal (7).
315 .TP
316 .B EINVAL
317 Invalid argument passed.
318 .TP
319 .B EISCONN
320 The connection-mode socket was connected already but a
321 recipient was specified.
322 (Now either this error is returned, or the recipient specification
323 is ignored.)
324 .TP
325 .B EMSGSIZE
326 The socket type
327 .\" (e.g., SOCK_DGRAM )
328 requires that message be sent atomically, and the size
329 of the message to be sent made this impossible.
330 .TP
331 .B ENOBUFS
332 The output queue for a network interface was full.
333 This generally indicates that the interface has stopped sending,
334 but may be caused by transient congestion.
335 (Normally, this does not occur in Linux.
336 Packets are just silently dropped
337 when a device queue overflows.)
338 .TP
339 .B ENOMEM
340 No memory available.
341 .TP
342 .B ENOTCONN
343 The socket is not connected, and no target has been given.
344 .TP
345 .B ENOTSOCK
346 The argument
347 .I sockfd
348 is not a socket.
349 .TP
350 .B EOPNOTSUPP
351 Some bit in the
352 .I flags
353 argument is inappropriate for the socket type.
354 .TP
355 .B EPIPE
356 The local end has been shut down on a connection oriented socket.
357 In this case the process
358 will also receive a
359 .B SIGPIPE
360 unless
361 .B MSG_NOSIGNAL
362 is set.
363 .SH "CONFORMING TO"
364 4.4BSD, SVr4, POSIX.1-2001.
365 These function calls appeared in 4.2BSD.
366 .LP
367 POSIX.1-2001 only describes the
368 .B MSG_OOB
369 and
370 .B MSG_EOR
371 flags.
372 POSIX.1-2008 adds a specification of
373 .BR MSG_NOSIGNAL .
374 The
375 .B MSG_CONFIRM
376 flag is a Linux extension.
377 .SH NOTES
378 The prototypes given above follow the Single UNIX Specification,
379 as glibc2 also does; the
380 .I flags
381 argument was \fIint\fP in 4.x BSD, but \fIunsigned int\fP in libc4 and libc5;
382 the
383 .I len
384 argument was \fIint\fP in 4.x BSD and libc4, but \fIsize_t\fP in libc5;
385 the
386 .I addrlen
387 argument was \fIint\fP in 4.x BSD and libc4 and libc5.
388 See also
389 .BR accept (2).
390
391 According to POSIX.1-2001, the
392 .I msg_controllen
393 field of the
394 .I msghdr
395 structure should be typed as
396 .IR socklen_t ,
397 but glibc currently types it as
398 .IR size_t .
399 .\" glibc bug raised 12 Mar 2006
400 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=2448
401 .\" The problem is an underlying kernel issue: the size of the
402 .\" __kernel_size_t type used to type this field varies
403 .\" across architectures, but socklen_t is always 32 bits.
404
405 See
406 .BR sendmmsg(2)
407 for information about a Linux-specific system call
408 that can be used to transmit multiple datagrams in a single call.
409 .SH BUGS
410 Linux may return
411 .B EPIPE
412 instead of
413 .BR ENOTCONN .
414 .SH EXAMPLE
415 An example of the use of
416 .BR sendto ()
417 is shown in
418 .BR getaddrinfo (3).
419 .SH "SEE ALSO"
420 .BR fcntl (2),
421 .BR getsockopt (2),
422 .BR recv (2),
423 .BR select (2),
424 .BR sendfile (2),
425 .BR sendmmsg (2),
426 .BR shutdown (2),
427 .BR socket (2),
428 .BR write (2),
429 .BR cmsg (3),
430 .BR ip (7),
431 .BR socket (7),
432 .BR tcp (7),
433 .BR udp (7)