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
64 The flags argument is the bitwise OR of zero or more of the following
65 flags.
66
67 MSG_CONFIRM (since Linux 2.3.15)
68 Tell the link layer that forward progress happened: you got a
69 successful reply from the other side. If the link layer doesn't
70 get this it will regularly reprobe the neighbor (e.g., via a
71 unicast ARP). Valid only on SOCK_DGRAM and SOCK_RAW sockets and
72 currently implemented only for IPv4 and IPv6. See arp(7) for
73 details.
74
75 MSG_DONTROUTE
76 Don't use a gateway to send out the packet, send to hosts only
77 on directly connected networks. This is usually used only by
78 diagnostic or routing programs. This is defined only for proto‐
79 col families that route; packet sockets don't.
80
81 MSG_DONTWAIT (since Linux 2.2)
82 Enables nonblocking operation; if the operation would block, EA‐
83 GAIN or EWOULDBLOCK is returned. This provides similar behavior
84 to setting the O_NONBLOCK flag (via the fcntl(2) F_SETFL opera‐
85 tion), but differs in that MSG_DONTWAIT is a per-call option,
86 whereas O_NONBLOCK is a setting on the open file description
87 (see open(2)), which will affect all threads in the calling
88 process and as well as other processes that hold file descrip‐
89 tors referring to the same open file description.
90
91 MSG_EOR (since Linux 2.2)
92 Terminates a record (when this notion is supported, as for sock‐
93 ets of type SOCK_SEQPACKET).
94
95 MSG_MORE (since Linux 2.4.4)
96 The caller has more data to send. This flag is used with TCP
97 sockets to obtain the same effect as the TCP_CORK socket option
98 (see tcp(7)), with the difference that this flag can be set on a
99 per-call basis.
100
101 Since Linux 2.6, this flag is also supported for UDP sockets,
102 and informs the kernel to package all of the data sent in calls
103 with this flag set into a single datagram which is transmitted
104 only when a call is performed that does not specify this flag.
105 (See also the UDP_CORK socket option described in udp(7).)
106
107 MSG_NOSIGNAL (since Linux 2.2)
108 Don't generate a SIGPIPE signal if the peer on a stream-oriented
109 socket has closed the connection. The EPIPE error is still re‐
110 turned. This provides similar behavior to using sigaction(2) to
111 ignore SIGPIPE, but, whereas MSG_NOSIGNAL is a per-call feature,
112 ignoring SIGPIPE sets a process attribute that affects all
113 threads in the process.
114
115 MSG_OOB
116 Sends out-of-band data on sockets that support this notion
117 (e.g., of type SOCK_STREAM); the underlying protocol must also
118 support out-of-band data.
119
120 sendmsg()
121 The definition of the msghdr structure employed by sendmsg() is as fol‐
122 lows:
123
124 struct msghdr {
125 void *msg_name; /* Optional address */
126 socklen_t msg_namelen; /* Size of address */
127 struct iovec *msg_iov; /* Scatter/gather array */
128 size_t msg_iovlen; /* # elements in msg_iov */
129 void *msg_control; /* Ancillary data, see below */
130 size_t msg_controllen; /* Ancillary data buffer len */
131 int msg_flags; /* Flags (unused) */
132 };
133
134 The msg_name field is used on an unconnected socket to specify the tar‐
135 get address for a datagram. It points to a buffer containing the ad‐
136 dress; the msg_namelen field should be set to the size of the address.
137 For a connected socket, these fields should be specified as NULL and 0,
138 respectively.
139
140 The msg_iov and msg_iovlen fields specify scatter-gather locations, as
141 for writev(2).
142
143 You may send control information (ancillary data) using the msg_control
144 and msg_controllen members. The maximum control buffer length the ker‐
145 nel can process is limited per socket by the value in
146 /proc/sys/net/core/optmem_max; see socket(7). For further information
147 on the use of ancillary data in various socket domains, see unix(7) and
148 ip(7).
149
150 The msg_flags field is ignored.
151
153 On success, these calls return the number of bytes sent. On error, -1
154 is returned, and errno is set appropriately.
155
157 These are some standard errors generated by the socket layer. Addi‐
158 tional errors may be generated and returned from the underlying proto‐
159 col modules; see their respective manual pages.
160
161 EACCES (For UNIX domain sockets, which are identified by pathname)
162 Write permission is denied on the destination socket file, or
163 search permission is denied for one of the directories the path
164 prefix. (See path_resolution(7).)
165
166 (For UDP sockets) An attempt was made to send to a net‐
167 work/broadcast address as though it was a unicast address.
168
169 EAGAIN or EWOULDBLOCK
170 The socket is marked nonblocking and the requested operation
171 would block. POSIX.1-2001 allows either error to be returned
172 for this case, and does not require these constants to have the
173 same value, so a portable application should check for both pos‐
174 sibilities.
175
176 EAGAIN (Internet domain datagram sockets) The socket referred to by
177 sockfd had not previously been bound to an address and, upon at‐
178 tempting to bind it to an ephemeral port, it was determined that
179 all port numbers in the ephemeral port range are currently in
180 use. See the discussion of /proc/sys/net/ipv4/ip_lo‐
181 cal_port_range in ip(7).
182
183 EALREADY
184 Another Fast Open is in progress.
185
186 EBADF sockfd is not a valid open file descriptor.
187
188 ECONNRESET
189 Connection reset by peer.
190
191 EDESTADDRREQ
192 The socket is not connection-mode, and no peer address is set.
193
194 EFAULT An invalid user space address was specified for an argument.
195
196 EINTR A signal occurred before any data was transmitted; see sig‐
197 nal(7).
198
199 EINVAL Invalid argument passed.
200
201 EISCONN
202 The connection-mode socket was connected already but a recipient
203 was specified. (Now either this error is returned, or the re‐
204 cipient specification is ignored.)
205
206 EMSGSIZE
207 The socket type requires that message be sent atomically, and
208 the size of the message to be sent made this impossible.
209
210 ENOBUFS
211 The output queue for a network interface was full. This gener‐
212 ally indicates that the interface has stopped sending, but may
213 be caused by transient congestion. (Normally, this does not oc‐
214 cur in Linux. Packets are just silently dropped when a device
215 queue overflows.)
216
217 ENOMEM No memory available.
218
219 ENOTCONN
220 The socket is not connected, and no target has been given.
221
222 ENOTSOCK
223 The file descriptor sockfd does not refer to a socket.
224
225 EOPNOTSUPP
226 Some bit in the flags argument is inappropriate for the socket
227 type.
228
229 EPIPE The local end has been shut down on a connection oriented
230 socket. In this case, the process will also receive a SIGPIPE
231 unless MSG_NOSIGNAL is set.
232
234 4.4BSD, SVr4, POSIX.1-2001. These interfaces first appeared in 4.2BSD.
235
236 POSIX.1-2001 describes only the MSG_OOB and MSG_EOR flags.
237 POSIX.1-2008 adds a specification of MSG_NOSIGNAL. The MSG_CONFIRM
238 flag is a Linux extension.
239
241 According to POSIX.1-2001, the msg_controllen field of the msghdr
242 structure should be typed as socklen_t, and the msg_iovlen field should
243 be typed as int, but glibc currently types both as size_t.
244
245 See sendmmsg(2) for information about a Linux-specific system call that
246 can be used to transmit multiple datagrams in a single call.
247
249 Linux may return EPIPE instead of ENOTCONN.
250
252 An example of the use of sendto() is shown in getaddrinfo(3).
253
255 fcntl(2), getsockopt(2), recv(2), select(2), sendfile(2), sendmmsg(2),
256 shutdown(2), socket(2), write(2), cmsg(3), ip(7), ipv6(7), socket(7),
257 tcp(7), udp(7), unix(7)
258
260 This page is part of release 5.10 of the Linux man-pages project. A
261 description of the project, information about reporting bugs, and the
262 latest version of this page, can be found at
263 https://www.kernel.org/doc/man-pages/.
264
265
266
267Linux 2020-11-01 SEND(2)