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. 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.\" From: @(#)send.2 8.2 (Berkeley) 2/21/94 33.\" $FreeBSD: src/lib/libc/sys/send.2,v 1.10.2.6 2001/12/14 18:34:01 ru Exp $ 34.\" 35.Dd October 6, 2010 36.Dt SEND 2 37.Os 38.Sh NAME 39.Nm send , 40.Nm sendto , 41.Nm sendmsg 42.Nd send a message from a socket 43.Sh LIBRARY 44.Lb libc 45.Sh SYNOPSIS 46.In sys/types.h 47.In sys/socket.h 48.Ft ssize_t 49.Fn send "int s" "const void *msgbuf" "size_t len" "int flags" 50.Ft ssize_t 51.Fn sendto "int s" "const void *msgbuf" "size_t len" "int flags" "const struct sockaddr *to" "socklen_t tolen" 52.Ft ssize_t 53.Fn sendmsg "int s" "const struct msghdr *msg" "int flags" 54.Sh DESCRIPTION 55.Fn Send , 56.Fn sendto , 57and 58.Fn sendmsg 59are used to transmit a message to another socket. 60.Fn Send 61may be used only when the socket is in a 62.Em connected 63state, while 64.Fn sendto 65and 66.Fn sendmsg 67may be used at any time. 68.Pp 69The socket file descriptor is given by 70.Fa s . 71.Fa msgbuf 72points to a buffer containing the message. 73.Fa msg 74points to a 75.Fa msghdr 76structure. 77The address of the target is given by 78.Fa to 79with 80.Fa tolen 81specifying its size. 82The length of the message is given by 83.Fa len . 84If the message is too long to pass atomically through the 85underlying protocol, the error 86.Er EMSGSIZE 87is returned, and 88the message is not transmitted. 89.Pp 90No indication of failure to deliver is implicit in a 91.Fn send . 92Locally detected errors are indicated by a return value of -1. 93.Pp 94If no messages space is available at the socket to hold 95the message to be transmitted, then 96.Fn send 97normally blocks, unless the socket has been placed in 98non-blocking I/O mode. 99The 100.Xr select 2 101call may be used to determine when it is possible to 102send more data. 103.Pp 104The 105.Fa flags 106parameter may include one or more of the following: 107.Bd -literal 108#define MSG_OOB 0x1 /* process out-of-band data */ 109#define MSG_PEEK 0x2 /* peek at incoming message */ 110#define MSG_DONTROUTE 0x4 /* bypass routing, use direct interface */ 111#define MSG_EOR 0x8 /* data completes record */ 112#define MSG_EOF 0x100 /* data completes transaction */ 113#define MSG_NOSIGNAL 0x400 /* No SIGPIPE to unconnected socket stream */ 114.Ed 115.Pp 116The flag 117.Dv MSG_OOB 118is used to send 119.Dq out-of-band 120data on sockets that support this notion (e.g.\& 121.Dv SOCK_STREAM ) ; 122the underlying protocol must also support 123.Dq out-of-band 124data. 125.Dv MSG_EOR 126is used to indicate a record mark for protocols which support the 127concept. 128.Dv MSG_EOF 129requests that the sender side of a socket be shut down, and that an 130appropriate indication be sent at the end of the specified data; 131this flag is only implemented for 132.Dv SOCK_STREAM 133sockets in the 134.Dv PF_INET 135protocol family. 136.Dv MSG_DONTROUTE 137is usually used only by diagnostic or routing programs. 138.Dv MSG_NOSIGNAL 139requests not to send the 140.Dv SIGPIPE 141signal if an attempt to 142.Nm 143is made on a stream-oriented socket that is no longer connected. 144.Pp 145See 146.Xr recv 2 147for a description of the 148.Fa msghdr 149structure. 150.Sh RETURN VALUES 151Upon successful completion the number of characters which were sent is 152returned. Otherwise -1 is returned and the global variable 153.Va errno 154is set to indicate the error. 155.Sh ERRORS 156.Fn Send , 157.Fn sendto , 158and 159.Fn sendmsg 160fail if: 161.Bl -tag -width Er 162.It Bq Er EBADF 163An invalid descriptor was specified. 164.It Bq Er EACCES 165The destination address is a broadcast address, and 166.Dv SO_BROADCAST 167has not been set on the socket. 168.It Bq Er ENOTSOCK 169The argument 170.Fa s 171is not a socket. 172.It Bq Er EFAULT 173An invalid user space address was specified for a parameter. 174.It Bq Er EMSGSIZE 175The socket requires that message be sent atomically, 176and the size of the message to be sent made this impossible. 177.It Bq Er EAGAIN 178The socket is marked non-blocking and the requested operation 179would block. 180.It Bq Er ENOBUFS 181The system was unable to allocate an internal buffer. 182The operation may succeed when buffers become available. 183.It Bq Er ENOBUFS 184The output queue for a network interface was full. 185This generally indicates that the interface has stopped sending, 186but may be caused by transient congestion. 187.It Bq Er EHOSTUNREACH 188The remote host was unreachable. 189.It Bq Er ECONNREFUSED 190The socket received an ICMP destination unreachable message 191from the last message sent. This typically means that the 192receiver is not listening on the remote port. 193.It Bq Er EHOSTDOWN 194The remote host was down. 195.It Bq Er EPIPE 196The socket is unable to send anymore data (SS_CANTSENDMORE has 197been set on the socket). This typically means that the socket 198is not connected. 199.El 200.Sh SEE ALSO 201.Xr fcntl 2 , 202.Xr getsockopt 2 , 203.Xr recv 2 , 204.Xr select 2 , 205.Xr socket 2 , 206.Xr write 2 207.Sh HISTORY 208The 209.Fn send 210function call appeared in 211.Bx 4.2 . 212.Sh BUGS 213Because 214.Fn sendmsg 215doesn't necessarily block until the data has been transferred, it 216is possible to transfer an open file descriptor across an 217.Dv AF_UNIX 218domain socket 219(see 220.Xr recv 2 ) , 221then 222.Fn close 223it before it has actually been sent, the result being that the receiver 224gets a closed file descriptor. It is left to the application to 225implement an acknowledgment mechanism to prevent this from happening. 226