1.\" Copyright (c) 1983, 1991, 1993 2.\" The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" From: @(#)send.2 8.2 (Berkeley) 2/21/94 29.\" $FreeBSD$ 30.\" 31.Dd April 27, 2020 32.Dt SEND 2 33.Os 34.Sh NAME 35.Nm send , 36.Nm sendto , 37.Nm sendmsg , 38.Nm sendmmsg 39.Nd send message(s) from a socket 40.Sh LIBRARY 41.Lb libc 42.Sh SYNOPSIS 43.In sys/socket.h 44.Ft ssize_t 45.Fn send "int s" "const void *msg" "size_t len" "int flags" 46.Ft ssize_t 47.Fn sendto "int s" "const void *msg" "size_t len" "int flags" "const struct sockaddr *to" "socklen_t tolen" 48.Ft ssize_t 49.Fn sendmsg "int s" "const struct msghdr *msg" "int flags" 50.Ft ssize_t 51.Fn sendmmsg "int s" "struct mmsghdr * restrict msgvec" "size_t vlen" "int flags" 52.Sh DESCRIPTION 53The 54.Fn send 55and 56.Fn sendmmsg 57functions, 58and 59.Fn sendto 60and 61.Fn sendmsg 62system calls 63are used to transmit one or more messages (with the 64.Fn sendmmsg 65call) to 66another socket. 67The 68.Fn send 69function 70may be used only when the socket is in a 71.Em connected 72state. 73The functions 74.Fn sendto , 75.Fn sendmsg 76and 77.Fn sendmmsg 78may be used at any time if the socket is connectionless-mode. 79If the socket is connection-mode, the protocol 80must support implied connect (currently 81.Xr tcp 4 82is the only protocol with support) or the socket must be in a 83connected state before use. 84.Pp 85The address of the target is given by 86.Fa to 87with 88.Fa tolen 89specifying its size, or the equivalent 90.Fa msg_name 91and 92.Fa msg_namelen 93in 94.Fa struct msghdr . 95If the socket is in a connected state, the target address passed to 96.Fn sendto , 97.Fn sendmsg 98or 99.Fn sendmmsg 100is ignored. 101The length of the message is given by 102.Fa len . 103If the message is too long to pass atomically through the 104underlying protocol, the error 105.Er EMSGSIZE 106is returned, and 107the message is not transmitted. 108.Pp 109The 110.Fn sendmmsg 111function sends multiple messages at a call. 112They are given by the 113.Fa msgvec 114vector along with 115.Fa vlen 116specifying the vector size. 117The number of octets sent per each message is placed in the 118.Fa msg_len 119field of each processed element of the vector after transmission. 120.Pp 121No indication of failure to deliver is implicit in a 122.Fn send . 123Locally detected errors are indicated by a return value of -1. 124.Pp 125If no messages space is available at the socket to hold 126the message to be transmitted, then 127.Fn send 128normally blocks, unless the socket has been placed in 129non-blocking I/O mode. 130The 131.Xr select 2 132system call may be used to determine when it is possible to 133send more data. 134.Pp 135The 136.Fa flags 137argument may include one or more of the following: 138.Bd -literal 139#define MSG_OOB 0x00001 /* process out-of-band data */ 140#define MSG_DONTROUTE 0x00004 /* bypass routing, use direct interface */ 141#define MSG_EOR 0x00008 /* data completes record */ 142#define MSG_DONTWAIT 0x00080 /* do not block */ 143#define MSG_EOF 0x00100 /* data completes transaction */ 144#define MSG_NOSIGNAL 0x20000 /* do not generate SIGPIPE on EOF */ 145.Ed 146.Pp 147The flag 148.Dv MSG_OOB 149is used to send 150.Dq out-of-band 151data on sockets that support this notion (e.g.\& 152.Dv SOCK_STREAM ) ; 153the underlying protocol must also support 154.Dq out-of-band 155data. 156.Dv MSG_EOR 157is used to indicate a record mark for protocols which support the 158concept. 159The 160.Dv MSG_DONTWAIT 161flag request the call to return when it would block otherwise. 162.Dv MSG_EOF 163requests that the sender side of a socket be shut down, and that an 164appropriate indication be sent at the end of the specified data; 165this flag is only implemented for 166.Dv SOCK_STREAM 167sockets in the 168.Dv PF_INET 169protocol family. 170.Dv MSG_DONTROUTE 171is usually used only by diagnostic or routing programs. 172.Dv MSG_NOSIGNAL 173is used to prevent 174.Dv SIGPIPE 175generation when writing a socket that 176may be closed. 177.Pp 178See 179.Xr recv 2 180for a description of the 181.Fa msghdr 182structure and the 183.Fa mmsghdr 184structure. 185.Sh RETURN VALUES 186The 187.Fn send , 188.Fn sendto 189and 190.Fn sendmsg 191calls 192return the number of octets sent. 193The 194.Fn sendmmsg 195call returns the number of messages sent. 196If an error occurred a value of -1 is returned. 197.Sh ERRORS 198The 199.Fn send 200and 201.Fn sendmmsg 202functions and 203.Fn sendto 204and 205.Fn sendmsg 206system calls 207fail if: 208.Bl -tag -width Er 209.It Bq Er EBADF 210An invalid descriptor was specified. 211.It Bq Er EACCES 212The destination address is a broadcast address, and 213.Dv SO_BROADCAST 214has not been set on the socket. 215.It Bq Er ENOTCONN 216The socket is connection-mode but is not connected. 217.It Bq Er ENOTSOCK 218The argument 219.Fa s 220is not a socket. 221.It Bq Er EFAULT 222An invalid user space address was specified for an argument. 223.It Bq Er EMSGSIZE 224The socket requires that message be sent atomically, 225and the size of the message to be sent made this impossible. 226.It Bq Er EAGAIN 227The socket is marked non-blocking, or 228.Dv MSG_DONTWAIT 229is specified, and the requested operation would block. 230.It Bq Er ENOBUFS 231The system was unable to allocate an internal buffer. 232The operation may succeed when buffers become available. 233.It Bq Er ENOBUFS 234The output queue for a network interface was full. 235This generally indicates that the interface has stopped sending, 236but may be caused by transient congestion. 237.It Bq Er EHOSTUNREACH 238The remote host was unreachable. 239.It Bq Er EISCONN 240A destination address was specified and the socket is already connected. 241.It Bq Er ECONNREFUSED 242The socket received an ICMP destination unreachable message 243from the last message sent. 244This typically means that the 245receiver is not listening on the remote port. 246.It Bq Er EHOSTDOWN 247The remote host was down. 248.It Bq Er ENETDOWN 249The remote network was down. 250.It Bq Er EADDRNOTAVAIL 251The process using a 252.Dv SOCK_RAW 253socket was jailed and the source 254address specified in the IP header did not match the IP 255address bound to the prison. 256.It Bq Er EPIPE 257The socket is unable to send anymore data 258.Dv ( SBS_CANTSENDMORE 259has been set on the socket). 260This typically means that the socket 261is not connected. 262.El 263.Sh SEE ALSO 264.Xr connect 2 , 265.Xr fcntl 2 , 266.Xr getsockopt 2 , 267.Xr recv 2 , 268.Xr select 2 , 269.Xr socket 2 , 270.Xr write 2 , 271.Xr CMSG_DATA 3 272.Sh HISTORY 273The 274.Fn send 275function appeared in 276.Bx 4.2 . 277The 278.Fn sendmmsg 279function appeared in 280.Fx 11.0 . 281.Sh BUGS 282Because 283.Fn sendmsg 284does not necessarily block until the data has been transferred, it 285is possible to transfer an open file descriptor across an 286.Dv AF_UNIX 287domain socket 288(see 289.Xr recv 2 ) , 290then 291.Fn close 292it before it has actually been sent, the result being that the receiver 293gets a closed file descriptor. 294It is left to the application to 295implement an acknowledgment mechanism to prevent this from happening. 296