Imported upstream version 1.5
[manpages-zh.git] / raw / man2 / send.2
blob2313041b5a67a167ff0802130821beacaac3a389
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 Sat Jul 24 01:15:33 1993 by Rik Faith <faith@cs.unc.edu>
33 .\" Modified Tue Oct 22 17:55:49 1996 by Eric S. Raymond <esr@thyrsus.com>
34 .\" Modified Oct 1998 by Andi Kleen
35 .\"
36 .TH SEND 2 2002-12-31 "Linux Man Page" "Linux Programmer's Manual"
37 .SH NAME
38 send, sendto, sendmsg \- send a message from a socket
39 .SH SYNOPSIS
40 .B #include <sys/types.h>
41 .br
42 .B #include <sys/socket.h>
43 .sp
44 .BI "ssize_t send(int " s ", const void *" msg ", size_t " len ,
45 .BI "int " flags );
46 .br
47 .BI "ssize_t sendto(int " s ", const void *" msg ", size_t " len ,
48 .BI "int " flags ", const struct sockaddr *" to ", socklen_t " tolen );
49 .br
50 .BI "ssize_t sendmsg(int " s ", const struct msghdr *" msg ,
51 .BI "int " flags );
52 .SH DESCRIPTION
53 .BR Send ,
54 .BR sendto ,
55 and
56 .B sendmsg
57 are used to transmit a message to another socket.
58 .B Send
59 may be used only when the socket is in a 
60 .I connected
61 state, while 
62 .B sendto
63 and
64 .B sendmsg
65 may be used at any time.
66 .PP
67 The address of the target is given by
68 .I to
69 with 
70 .I tolen
71 specifying its size.  The length of the message is given by
72 .IR len .
73 If the message is too long to pass atomically through the
74 underlying protocol, the error
75 .B EMSGSIZE
76 is returned, and the message is not transmitted.
77 .PP
78 No indication of failure to deliver is implicit in a
79 .BR send .
80 Locally detected errors are indicated by a return value of \-1.
81 .PP
82 When the message does not fit into the send buffer of the socket,
83 .B send
84 normally blocks, unless the socket has been placed in non-blocking I/O
85 mode.  In non-blocking mode it would return
86 .B EAGAIN
87 in this case.
88 The
89 .BR select (2)
90 call may be used to determine when it is possible to send more data.
91 .PP
92 The
93 .I flags
94 parameter is a flagword and can contain the following flags:
95 .\" XXX document MSG_PROXY
96 .TP
97 .B MSG_OOB
98 Sends
99 .I out-of-band
100 data on sockets that support this notion (e.g.
101 .BR SOCK_STREAM );
102 the underlying protocol must also support
103 .I out-of-band
104 data.
106 .B MSG_DONTROUTE
107 Dont't use a gateway to send out the packet, only send to hosts on 
108 directly connected networks. This is usually used only 
109 by diagnostic or routing programs. This is only defined for protocol
110 families that route; packet sockets don't.
112 .B MSG_DONTWAIT
113 Enables non-blocking operation; if the operation would block,
114 .B EAGAIN
115 is returned (this can also be enabled using the
116 .B O_NONBLOCK
117 with the
118 .B F_SETFL
119 .BR fcntl (2)).
121 .B MSG_NOSIGNAL
122 Requests not to send 
123 .B SIGPIPE 
124 on errors on stream oriented sockets when the other end breaks the
125 connection. The 
126 .B EPIPE
127 error is still returned.
129 .BR MSG_CONFIRM " (Linux 2.3+ only)"
130 Tell the link layer that forward process happened: you got a successful
131 reply from the other side. If the link layer doesn't get this 
132 it'll regularly reprobe the neighbour (e.g. via a unicast ARP).
133 Only valid on 
134 .B SOCK_DGRAM
136 .B SOCK_RAW
137 sockets and currently only implemented for IPv4 and IPv6. See
138 .BR arp (7)
139 for details.
141 The definition of the
142 .I msghdr
143 structure follows. See 
144 .BR recv (2)
145 and below for an exact description of its fields.
149 .ta 4n 17n 33n
150 struct msghdr {
151         void    * msg_name;     /* optional address */
152         socklen_t       msg_namelen;    /* size of address */
153         struct iovec    * msg_iov;      /* scatter/gather array */
154         size_t  msg_iovlen;     /* # elements in msg_iov */
155         void    * msg_control;  /* ancillary data, see below */
156         socklen_t       msg_controllen; /* ancillary data buffer len */
157         int     msg_flags;      /* flags on received message */
163 You may send control information using the 
164 .I msg_control 
165 and 
166 .I msg_controllen 
167 members. The maximum control buffer length the kernel can process is limited
168 per socket by the
169 .B net.core.optmem_max 
170 sysctl; see
171 .BR socket (7).
172 .SH "RETURN VALUE"
173 The calls return the number of characters sent, or \-1
174 if an error occurred.
175 .SH ERRORS
176 These are some standard errors generated by the socket layer. Additional errors
177 may be generated and returned from the underlying protocol modules; see their
178 respective manual pages.
180 .B EBADF
181 An invalid descriptor was specified.
183 .B ENOTSOCK
184 The argument
185 .I s
186 is not a socket.
188 .B EFAULT
189 An invalid user space address was specified for a parameter.
191 .B EMSGSIZE
192 The socket requires that message be sent atomically, and the size
193 of the message to be sent made this impossible.
195 .BR EAGAIN " or " EWOULDBLOCK
196 The socket is marked non-blocking and the requested operation
197 would block.
199 .B ENOBUFS
200 The output queue for a network interface was full.
201 This generally indicates that the interface has stopped sending,
202 but may be caused by transient congestion.
203 (Normally, this does not occur in Linux. Packets are just silently dropped
204 when a device queue overflows.)
206 .B EINTR
207 A signal occurred.
209 .B ENOMEM
210 No memory available.
212 .B EINVAL
213 Invalid argument passed. 
215 .B EPIPE
216 The local end has been shut down on a connection oriented socket.
217 In this case the process
218 will also receive a 
219 .B SIGPIPE 
220 unless 
221 .B MSG_NOSIGNAL 
222 is set.
223 .SH "CONFORMING TO"
224 4.4BSD, SVr4, POSIX 1003.1g draft (these function calls appeared in 4.2BSD).
226 .B MSG_CONFIRM 
227 is a Linux extension.
228 .SH NOTE
229 The prototypes given above follow the Single Unix Specification,
230 as glibc2 also does; the
231 .I flags
232 argument was `int' in BSD 4.*, but `unsigned int' in libc4 and libc5;
234 .I len
235 argument was `int' in BSD 4.* and libc4, but `size_t' in libc5;
237 .I tolen
238 argument was `int' in BSD 4.* and libc4 and libc5.
239 See also
240 .BR accept (2).
241 .SH "SEE ALSO"
242 .BR fcntl (2),
243 .BR recv (2),
244 .BR select (2),
245 .BR getsockopt (2),
246 .BR sendfile (2),
247 .BR socket (2),
248 .BR write (2),
249 .BR socket (7),
250 .BR ip (7),
251 .BR tcp (7),
252 .BR udp (7)