1RECV(3P)                   POSIX Programmer's Manual                  RECV(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       recv — receive a message from a connected socket
14

SYNOPSIS

16       #include <sys/socket.h>
17
18       ssize_t recv(int socket, void *buffer, size_t length, int flags);
19

DESCRIPTION

21       The recv() function shall receive a message from a  connection-mode  or
22       connectionless-mode  socket. It is normally used with connected sockets
23       because it does not permit  the  application  to  retrieve  the  source
24       address of received data.
25
26       The recv() function takes the following arguments:
27
28       socket    Specifies the socket file descriptor.
29
30       buffer    Points to a buffer where the message should be stored.
31
32       length    Specifies the length in bytes of the buffer pointed to by the
33                 buffer argument.
34
35       flags     Specifies the type of message reception. Values of this argu‐
36                 ment  are formed by logically OR'ing zero or more of the fol‐
37                 lowing values:
38
39                 MSG_PEEK    Peeks at an incoming message. The data is treated
40                             as unread and the next recv() or similar function
41                             shall still return this data.
42
43                 MSG_OOB     Requests out-of-band data. The  significance  and
44                             semantics  of  out-of-band data are protocol-spe‐
45                             cific.
46
47                 MSG_WAITALL On SOCK_STREAM sockets  this  requests  that  the
48                             function  block until the full amount of data can
49                             be returned. The function may return the  smaller
50                             amount  of  data if the socket is a message-based
51                             socket, if a signal is caught, if the  connection
52                             is  terminated,  if MSG_PEEK was specified, or if
53                             an error is pending for the socket.
54
55       The recv() function shall return the length of the message  written  to
56       the  buffer  pointed to by the buffer argument. For message-based sock‐
57       ets, such as SOCK_DGRAM and SOCK_SEQPACKET, the entire message shall be
58       read  in  a  single  operation.  If a message is too long to fit in the
59       supplied buffer, and MSG_PEEK is not set in  the  flags  argument,  the
60       excess  bytes  shall  be  discarded.  For stream-based sockets, such as
61       SOCK_STREAM, message boundaries shall be ignored. In  this  case,  data
62       shall  be  returned to the user as soon as it becomes available, and no
63       data shall be discarded.
64
65       If the MSG_WAITALL flag is not set, data shall be returned only  up  to
66       the end of the first message.
67
68       If no messages are available at the socket and O_NONBLOCK is not set on
69       the socket's file  descriptor,  recv()  shall  block  until  a  message
70       arrives.  If  no messages are available at the socket and O_NONBLOCK is
71       set on the socket's file descriptor, recv() shall fail and set errno to
72       [EAGAIN] or [EWOULDBLOCK].
73

RETURN VALUE

75       Upon  successful completion, recv() shall return the length of the mes‐
76       sage in bytes. If no messages are available to be received and the peer
77       has performed an orderly shutdown, recv() shall return 0. Otherwise, −1
78       shall be returned and errno set to indicate the error.
79

ERRORS

81       The recv() function shall fail if:
82
83       EAGAIN or EWOULDBLOCK
84              The socket's file descriptor is marked O_NONBLOCK and no data is
85              waiting  to  be  received;  or MSG_OOB is set and no out-of-band
86              data is available and either the  socket's  file  descriptor  is
87              marked  O_NONBLOCK  or  the  socket does not support blocking to
88              await out-of-band data.
89
90       EBADF  The socket argument is not a valid file descriptor.
91
92       ECONNRESET
93              A connection was forcibly closed by a peer.
94
95       EINTR  The recv() function was interrupted by a signal that was caught,
96              before any data was available.
97
98       EINVAL The MSG_OOB flag is set and no out-of-band data is available.
99
100       ENOTCONN
101              A  receive  is attempted on a connection-mode socket that is not
102              connected.
103
104       ENOTSOCK
105              The socket argument does not refer to a socket.
106
107       EOPNOTSUPP
108              The specified flags are not supported for this  socket  type  or
109              protocol.
110
111       ETIMEDOUT
112              The connection timed out during connection establishment, or due
113              to a transmission timeout on active connection.
114
115       The recv() function may fail if:
116
117       EIO    An I/O error occurred while reading from or writing to the  file
118              system.
119
120       ENOBUFS
121              Insufficient  resources  were available in the system to perform
122              the operation.
123
124       ENOMEM Insufficient memory was available to fulfill the request.
125
126       The following sections are informative.
127

EXAMPLES

129       None.
130

APPLICATION USAGE

132       The recv() function is  equivalent  to  recvfrom()  with  null  pointer
133       address and address_len arguments, and to read() if the socket argument
134       refers to a socket and the flags argument is 0.
135
136       The select() and poll() functions can be used to determine when data is
137       available to be received.
138

RATIONALE

140       None.
141

FUTURE DIRECTIONS

143       None.
144

SEE ALSO

146       poll(),  pselect(),  read(),  recvmsg(), recvfrom(), send(), sendmsg(),
147       sendto(), shutdown(), socket(), write()
148
149       The Base Definitions volume of POSIX.1‐2008, <sys_socket.h>
150
152       Portions of this text are reprinted and reproduced in  electronic  form
153       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
154       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
155       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
156       cal and Electronics Engineers,  Inc  and  The  Open  Group.   (This  is
157       POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
158       event of any discrepancy between this version and the original IEEE and
159       The  Open Group Standard, the original IEEE and The Open Group Standard
160       is the referee document. The original Standard can be  obtained  online
161       at http://www.unix.org/online.html .
162
163       Any  typographical  or  formatting  errors that appear in this page are
164       most likely to have been introduced during the conversion of the source
165       files  to  man page format. To report such errors, see https://www.ker
166       nel.org/doc/man-pages/reporting_bugs.html .
167
168
169
170IEEE/The Open Group                  2013                             RECV(3P)
Impressum