161183Sbostic.\" Copyright (c) 1983, 1990, 1991, 1993 261183Sbostic.\" The Regents of the University of California. All rights reserved. 320249Smckusick.\" 442475Ssklower.\" %sccs.include.redist.man% 520249Smckusick.\" 6*66183Sbostic.\" @(#)recv.2 8.3 (Berkeley) 02/21/94 736759Sbostic.\" 847208Scael.Dd 947208Scael.Dt RECV 2 1047988Skarels.Os BSD 4.3r 1147208Scael.Sh NAME 1247208Scael.Nm recv , 1347208Scael.Nm recvfrom , 1447208Scael.Nm recvmsg 1547208Scael.Nd receive a message from a socket 1647208Scael.Sh SYNOPSIS 1747208Scael.Fd #include <sys/types.h> 1847208Scael.Fd #include <sys/socket.h> 19*66183Sbostic.Ft ssize_t 20*66183Sbostic.Fn recv "int s" "void *buf" "size_t len" "int flags" 21*66183Sbostic.Ft ssize_t 22*66183Sbostic.Fn recvfrom "int s" "void *buf" "size_t len" "int flags" "struct sockaddr *from" "int *fromlen" 23*66183Sbostic.Ft ssize_t 2447208Scael.Fn recvmsg "int s" "struct msghdr *msg" "int flags" 2547208Scael.Sh DESCRIPTION 2647208Scael.Fn Recvfrom 2720249Smckusickand 2847208Scael.Fn recvmsg 2942473Ssklowerare used to receive messages from a socket, 3047988Skarelsand may be used to receive data on a socket whether or not 3147988Skarelsit is connection-oriented. 3247208Scael.Pp 3320249SmckusickIf 3447208Scael.Fa from 3547988Skarelsis non-nil, and the socket is not connection-oriented, 3647988Skarelsthe source address of the message is filled in. 3747208Scael.Fa Fromlen 3820249Smckusickis a value-result parameter, initialized to the size of 3920249Smckusickthe buffer associated with 4047208Scael.Fa from , 4120249Smckusickand modified on return to indicate the actual size of the 4220249Smckusickaddress stored there. 4347208Scael.Pp 4442473SsklowerThe 4547208Scael.Fn recv 4642473Ssklowercall is normally used only on a 4747208Scael.Em connected 4842473Ssklowersocket (see 4947208Scael.Xr connect 2 ) 5047208Scaeland is identical to 5147208Scael.Fn recvfrom 5247208Scaelwith a nil 5347208Scael.Fa from 5442473Ssklowerparameter. 5542473SsklowerAs it is redundant, it may not be supported in future releases. 5647208Scael.Pp 5747208ScaelAll three routines return the length of the message on successful 5847208Scaelcompletion. 5920249SmckusickIf a message is too long to fit in the supplied buffer, 6020249Smckusickexcess bytes may be discarded depending on the type of socket 6128639Skjdthe message is received from (see 6247208Scael.Xr socket 2 ) . 6347208Scael.Pp 6420249SmckusickIf no messages are available at the socket, the 6520249Smckusickreceive call waits for a message to arrive, unless 6620249Smckusickthe socket is nonblocking (see 6747988Skarels.Xr fcntl 2 ) 6847208Scaelin which case the value 6947208Scael-1 is returned and the external variable 7047208Scael.Va errno 7147208Scaelset to 7253310Sbostic.Er EAGAIN . 7347988SkarelsThe receive calls normally return any data available, 7447988Skarelsup to the requested amount, 7547988Skarelsrather than waiting for receipt of the full amount requested; 7647988Skarelsthis behavior is affected by the socket-level options 7747988Skarels.Dv SO_RCVLOWAT 7847988Skarelsand 7947988Skarels.Dv SO_RCVTIMEO 8047988Skarelsdescribed in 8147988Skarels.Xr getsockopt 2 . 8247208Scael.Pp 8320249SmckusickThe 8447208Scael.Xr select 2 8547988Skarelscall may be used to determine when more data arrive. 8647208Scael.Pp 8720249SmckusickThe 8847208Scael.Fa flags 8928126Skarelsargument to a recv call is formed by 9047208Scael.Em or Ap ing 9147208Scaelone or more of the values: 9247988Skarels.Bl -column MSG_WAITALL -offset indent 9347988Skarels.It Dv MSG_OOB Ta process out-of-band data 9447988Skarels.It Dv MSG_PEEK Ta peek at incoming message 9547988Skarels.It Dv MSG_WAITALL Ta wait for full request or error 9647988Skarels.El 9747988SkarelsThe 9847988Skarels.Dv MSG_OOB 9947988Skarelsflag requests receipt of out-of-band data 10047988Skarelsthat would not be received in the normal data stream. 10147988SkarelsSome protocols place expedited data at the head of the normal 10247988Skarelsdata queue, and thus this flag cannot be used with such protocols. 10347988SkarelsThe MSG_PEEK flag causes the receive operation to return data 10447988Skarelsfrom the beginning of the receive queue without removing that 10547988Skarelsdata from the queue. 10647988SkarelsThus, a subsequent receive call will return the same data. 10747988SkarelsThe MSG_WAITALL flag requests that the operation block until 10847988Skarelsthe full request is satisfied. 10947988SkarelsHowever, the call may still return less data than requested 11047988Skarelsif a signal is caught, an error or disconnect occurs, 11147988Skarelsor the next data to be received is of a different type than that returned. 11247208Scael.Pp 11320249SmckusickThe 11447208Scael.Fn recvmsg 11520249Smckusickcall uses a 11647208Scael.Fa msghdr 11720249Smckusickstructure to minimize the number of directly supplied parameters. 11820249SmckusickThis structure has the following form, as defined in 11947208Scael.Ao Pa sys/socket.h Ac : 12047208Scael.Pp 12147208Scael.Bd -literal 12220249Smckusickstruct msghdr { 12342473Ssklower caddr_t msg_name; /* optional address */ 12442473Ssklower u_int msg_namelen; /* size of address */ 12542473Ssklower struct iovec *msg_iov; /* scatter/gather array */ 12642473Ssklower u_int msg_iovlen; /* # elements in msg_iov */ 12742473Ssklower caddr_t msg_control; /* ancillary data, see below */ 12847208Scael u_int msg_controllen; /* ancillary data buffer len */ 12947208Scael int msg_flags; /* flags on received message */ 13020249Smckusick}; 13147208Scael.Ed 13247208Scael.Pp 13320249SmckusickHere 13447208Scael.Fa msg_name 13520249Smckusickand 13647208Scael.Fa msg_namelen 13720249Smckusickspecify the destination address if the socket is unconnected; 13847208Scael.Fa msg_name 13920249Smckusickmay be given as a null pointer if no names are desired or required. 14047208Scael.Fa Msg_iov 14120249Smckusickand 14247208Scael.Fa msg_iovlen 14347208Scaeldescribe scatter gather locations, as discussed in 14447208Scael.Xr read 2 . 14547208Scael.Fa Msg_control , 14620249Smckusickwhich has length 14747208Scael.Fa msg_controllen , 14847208Scaelpoints to a buffer for other protocol control related messages 14942473Sskloweror other miscellaneous ancillary data. 15042473SsklowerThe messages are of the form: 15147208Scael.Bd -literal 15242473Ssklowerstruct cmsghdr { 15342473Ssklower u_int cmsg_len; /* data byte count, including hdr */ 15442473Ssklower int cmsg_level; /* originating protocol */ 15542473Ssklower int cmsg_type; /* protocol-specific type */ 15642473Ssklower/* followed by 15742473Ssklower u_char cmsg_data[]; */ 15842473Ssklower}; 15947208Scael.Ed 16042473SsklowerAs an example, one could use this to learn of changes in the data-stream 16142473Ssklowerin XNS/SPP, or in ISO, to obtain user-connection-request data by requesting 16247988Skarelsa recvmsg with no data buffer provided immediately after an 16347988Skarels.Fn accept 16447988Skarelscall. 16547208Scael.Pp 16647208ScaelOpen file descriptors are now passed as ancillary data for 16747208Scael.Dv AF_UNIX 16847208Scaeldomain sockets, with 16947208Scael.Fa cmsg_level 17047988Skarelsset to 17147208Scael.Dv SOL_SOCKET 17247208Scaeland 17347208Scael.Fa cmsg_type 17447988Skarelsset to 17547208Scael.Dv SCM_RIGHTS . 17647208Scael.Pp 17747988SkarelsThe 17847988Skarels.Fa msg_flags 17947988Skarelsfield is set on return according to the message received. 18047208Scael.Dv MSG_EOR 18147988Skarelsindicates end-of-record; 18247988Skarelsthe data returned completed a record (generally used with sockets of type 18347988Skarels.Dv SOCK_SEQPACKET ) . 18447208Scael.Dv MSG_TRUNC 18547208Scaelindicates that 18647988Skarelsthe trailing portion of a datagram was discarded because the datagram 18747988Skarelswas larger than the buffer supplied. 18847208Scael.Dv MSG_CTRUNC 18947208Scaelindicates that some 19047988Skarelscontrol data were discarded due to lack of space in the buffer 19147988Skarelsfor ancillary data. 19247208Scael.Dv MSG_OOB 19347988Skarelsis returned to indicate that expedited or out-of-band data were received. 19447208Scael.Pp 19547208Scael.Sh RETURN VALUES 19647208ScaelThese calls return the number of bytes received, or -1 19720249Smckusickif an error occurred. 19847208Scael.Sh ERRORS 19920249SmckusickThe calls fail if: 20053310Sbostic.Bl -tag -width ENOTCONNAA 20147208Scael.It Bq Er EBADF 20247208ScaelThe argument 20347208Scael.Fa s 20447208Scaelis an invalid descriptor. 20547988Skarels.It Bq Er ENOTCONN 20665100SmckusickThe socket is associated with a connection-oriented protocol 20747988Skarelsand has not been connected (see 20847988Skarels.Xr connect 2 20947988Skarelsand 21047988Skarels.Xr accept 2 ). 21147208Scael.It Bq Er ENOTSOCK 21247208ScaelThe argument 21347208Scael.Fa s 21447988Skarelsdoes not refer to a socket. 21553310Sbostic.It Bq Er EAGAIN 21647988SkarelsThe socket is marked non-blocking, and the receive operation 21747988Skarelswould block, or 21847988Skarelsa receive timeout had been set, 21947988Skarelsand the timeout expired before data were received. 22047208Scael.It Bq Er EINTR 22120249SmckusickThe receive was interrupted by delivery of a signal before 22247988Skarelsany data were available. 22347208Scael.It Bq Er EFAULT 22447208ScaelThe receive buffer pointer(s) point outside the process's 22547208Scaeladdress space. 22647208Scael.El 22747208Scael.Sh SEE ALSO 22847208Scael.Xr fcntl 2 , 22947208Scael.Xr read 2 , 23047208Scael.Xr select 2 , 23147208Scael.Xr getsockopt 2 , 23247208Scael.Xr socket 2 23347208Scael.Sh HISTORY 23447208ScaelThe 23547208Scael.Nm 23647208Scaelfunction call appeared in 23747208Scael.Bx 4.2 . 238