1SEND(2) Linux Programmer's Manual SEND(2)
2
3
4
6 send, sendto, sendmsg - send a message on a socket
7
9 #include <sys/types.h>
10 #include <sys/socket.h>
11
12 ssize_t send(int sockfd, const void *buf, size_t len, int flags);
13
14 ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
15 const struct sockaddr *dest_addr, socklen_t addrlen);
16
17 ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
18
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 is the bitwise OR of zero or more of the following
64 flags.
65
66 MSG_CONFIRM (Since Linux 2.3.15)
67 Tell the link layer that forward progress happened: you got a
68 successful reply from the other side. If the link layer doesn't
69 get this it will regularly reprobe the neighbor (e.g., via a
70 unicast ARP). Only valid on SOCK_DGRAM and SOCK_RAW sockets and
71 currently implemented only for IPv4 and IPv6. See arp(7) for
72 details.
73
74 MSG_DONTROUTE
75 Don't use a gateway to send out the packet, send to hosts only
76 on directly connected networks. This is usually used only by
77 diagnostic or routing programs. This is defined only for proto‐
78 col families that route; packet sockets don't.
79
80 MSG_DONTWAIT (since Linux 2.2)
81 Enables nonblocking operation; if the operation would block,
82 EAGAIN or EWOULDBLOCK is returned (this can also be enabled
83 using the O_NONBLOCK flag with the F_SETFL fcntl(2)).
84
85 MSG_EOR (since Linux 2.2)
86 Terminates a record (when this notion is supported, as for sock‐
87 ets of type SOCK_SEQPACKET).
88
89 MSG_MORE (Since Linux 2.4.4)
90 The caller has more data to send. This flag is used with TCP
91 sockets to obtain the same effect as the TCP_CORK socket option
92 (see tcp(7)), with the difference that this flag can be set on a
93 per-call basis.
94
95 Since Linux 2.6, this flag is also supported for UDP sockets,
96 and informs the kernel to package all of the data sent in calls
97 with this flag set into a single datagram which is transmitted
98 only when a call is performed that does not specify this flag.
99 (See also the UDP_CORK socket option described in udp(7).)
100
101 MSG_NOSIGNAL (since Linux 2.2)
102 Requests not to send SIGPIPE on errors on stream oriented sock‐
103 ets when the other end breaks the connection. The EPIPE error
104 is still returned.
105
106 MSG_OOB
107 Sends out-of-band data on sockets that support this notion
108 (e.g., of type SOCK_STREAM); the underlying protocol must also
109 support out-of-band data.
110
111 The definition of the msghdr structure follows. See recv(2) and below
112 for an exact description of its fields.
113
114 struct msghdr {
115 void *msg_name; /* optional address */
116 socklen_t msg_namelen; /* size of address */
117 struct iovec *msg_iov; /* scatter/gather array */
118 size_t msg_iovlen; /* # elements in msg_iov */
119 void *msg_control; /* ancillary data, see below */
120 size_t msg_controllen; /* ancillary data buffer len */
121 int msg_flags; /* flags on received message */
122 };
123
124 You may send control information using the msg_control and msg_con‐
125 trollen members. The maximum control buffer length the kernel can
126 process is limited per socket by the value in /proc/sys/net/core/opt‐
127 mem_max; see socket(7).
128
130 On success, these calls return the number of characters sent. On
131 error, -1 is returned, and errno is set appropriately.
132
134 These are some standard errors generated by the socket layer. Addi‐
135 tional errors may be generated and returned from the underlying proto‐
136 col modules; see their respective manual pages.
137
138 EACCES (For UNIX domain sockets, which are identified by pathname)
139 Write permission is denied on the destination socket file, or
140 search permission is denied for one of the directories the path
141 prefix. (See path_resolution(7).)
142
143 (For UDP sockets) An attempt was made to send to a net‐
144 work/broadcast address as though it was a unicast address.
145
146 EAGAIN or EWOULDBLOCK
147 The socket is marked nonblocking and the requested operation
148 would block. POSIX.1-2001 allows either error to be returned
149 for this case, and does not require these constants to have the
150 same value, so a portable application should check for both pos‐
151 sibilities.
152
153 EBADF An invalid descriptor was specified.
154
155 ECONNRESET
156 Connection reset by peer.
157
158 EDESTADDRREQ
159 The socket is not connection-mode, and no peer address is set.
160
161 EFAULT An invalid user space address was specified for an argument.
162
163 EINTR A signal occurred before any data was transmitted; see sig‐
164 nal(7).
165
166 EINVAL Invalid argument passed.
167
168 EISCONN
169 The connection-mode socket was connected already but a recipient
170 was specified. (Now either this error is returned, or the
171 recipient specification is ignored.)
172
173 EMSGSIZE
174 The socket type requires that message be sent atomically, and
175 the size of the message to be sent made this impossible.
176
177 ENOBUFS
178 The output queue for a network interface was full. This gener‐
179 ally indicates that the interface has stopped sending, but may
180 be caused by transient congestion. (Normally, this does not
181 occur in Linux. Packets are just silently dropped when a device
182 queue overflows.)
183
184 ENOMEM No memory available.
185
186 ENOTCONN
187 The socket is not connected, and no target has been given.
188
189 ENOTSOCK
190 The argument sockfd is not a socket.
191
192 EOPNOTSUPP
193 Some bit in the flags argument is inappropriate for the socket
194 type.
195
196 EPIPE The local end has been shut down on a connection oriented
197 socket. In this case the process will also receive a SIGPIPE
198 unless MSG_NOSIGNAL is set.
199
201 4.4BSD, SVr4, POSIX.1-2001. These function calls appeared in 4.2BSD.
202
203 POSIX.1-2001 describes only the MSG_OOB and MSG_EOR flags.
204 POSIX.1-2008 adds a specification of MSG_NOSIGNAL. The MSG_CONFIRM
205 flag is a Linux extension.
206
208 The prototypes given above follow the Single UNIX Specification, as
209 glibc2 also does; the flags argument was int in 4.x BSD, but unsigned
210 int in libc4 and libc5; the len argument was int in 4.x BSD and libc4,
211 but size_t in libc5; the addrlen argument was int in 4.x BSD and libc4
212 and libc5. See also accept(2).
213
214 According to POSIX.1-2001, the msg_controllen field of the msghdr
215 structure should be typed as socklen_t, but glibc currently types it as
216 size_t.
217
218 See sendmmsg(2) for information about a Linux-specific system call that
219 can be used to transmit multiple datagrams in a single call.
220
222 Linux may return EPIPE instead of ENOTCONN.
223
225 An example of the use of sendto() is shown in getaddrinfo(3).
226
228 fcntl(2), getsockopt(2), recv(2), select(2), sendfile(2), sendmmsg(2),
229 shutdown(2), socket(2), write(2), cmsg(3), ip(7), socket(7), tcp(7),
230 udp(7)
231
233 This page is part of release 3.53 of the Linux man-pages project. A
234 description of the project, and information about reporting bugs, can
235 be found at http://www.kernel.org/doc/man-pages/.
236
237
238
239Linux 2012-04-23 SEND(2)