1RECVFROM(3P)               POSIX Programmer's Manual              RECVFROM(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

NAME

12       recvfrom — receive a message from a socket
13

SYNOPSIS

15       #include <sys/socket.h>
16
17       ssize_t recvfrom(int socket, void *restrict buffer, size_t length,
18           int flags, struct sockaddr *restrict address,
19           socklen_t *restrict address_len);
20

DESCRIPTION

22       The recvfrom() function shall receive a message from a  connection-mode
23       or connectionless-mode socket. It is normally used with connectionless-
24       mode sockets because it permits the application to retrieve the  source
25       address of received data.
26
27       The recvfrom() function takes the following arguments:
28
29       socket      Specifies the socket file descriptor.
30
31       buffer      Points to the buffer where the message should be stored.
32
33       length      Specifies  the  length in bytes of the buffer pointed to by
34                   the buffer argument.
35
36       flags       Specifies the type of message  reception.  Values  of  this
37                   argument are formed by logically OR'ing zero or more of the
38                   following values:
39
40                   MSG_PEEK    Peeks at  an  incoming  message.  The  data  is
41                               treated  as  unread  and the next recvfrom() or
42                               similar function shall still return this data.
43
44                   MSG_OOB     Requests out-of-band data. The significance and
45                               semantics of out-of-band data are protocol-spe‐
46                               cific.
47
48                   MSG_WAITALL On SOCK_STREAM sockets this requests  that  the
49                               function  block  until  the full amount of data
50                               can be returned. The function  may  return  the
51                               smaller  amount of data if the socket is a mes‐
52                               sage-based socket, if a signal  is  caught,  if
53                               the  connection  is terminated, if MSG_PEEK was
54                               specified, or if an error is  pending  for  the
55                               socket.
56
57       address     A  null pointer, or points to a sockaddr structure in which
58                   the sending address is to be stored. The length and  format
59                   of the address depend on the address family of the socket.
60
61       address_len Either  a  null pointer, if address is a null pointer, or a
62                   pointer to a socklen_t object which on input specifies  the
63                   length  of  the  supplied sockaddr structure, and on output
64                   specifies the length of the stored address.
65
66       The recvfrom() function shall return the length of the message  written
67       to  the  buffer  pointed  to  by the buffer argument. For message-based
68       sockets, such as SOCK_RAW, SOCK_DGRAM, and SOCK_SEQPACKET,  the  entire
69       message  shall  be read in a single operation. If a message is too long
70       to fit in the supplied buffer, and MSG_PEEK is not  set  in  the  flags
71       argument,  the  excess bytes shall be discarded. For stream-based sock‐
72       ets, such as SOCK_STREAM, message boundaries shall be ignored. In  this
73       case,  data  shall be returned to the user as soon as it becomes avail‐
74       able, and no data shall be discarded.
75
76       If the MSG_WAITALL flag is not set, data shall be returned only  up  to
77       the end of the first message.
78
79       Not  all  protocols  provide  the  source  address for messages. If the
80       address argument is not a null pointer and the  protocol  provides  the
81       source  address of messages, the source address of the received message
82       shall be stored in the sockaddr structure pointed  to  by  the  address
83       argument,  and the length of this address shall be stored in the object
84       pointed to by the address_len argument.
85
86       If the actual length of the address is greater than the length  of  the
87       supplied sockaddr structure, the stored address shall be truncated.
88
89       If the address argument is not a null pointer and the protocol does not
90       provide the source address of messages, the value stored in the  object
91       pointed to by address is unspecified.
92
93       If no messages are available at the socket and O_NONBLOCK is not set on
94       the socket's file descriptor, recvfrom() shall block  until  a  message
95       arrives.  If  no messages are available at the socket and O_NONBLOCK is
96       set on the socket's file descriptor,  recvfrom()  shall  fail  and  set
97       errno to [EAGAIN] or [EWOULDBLOCK].
98

RETURN VALUE

100       Upon  successful  completion, recvfrom() shall return the length of the
101       message in bytes. If no messages are available to be received  and  the
102       peer has performed an orderly shutdown, recvfrom() shall return 0. Oth‐
103       erwise, the function shall return -1 and  set  errno  to  indicate  the
104       error.
105

ERRORS

107       The recvfrom() function shall fail if:
108
109       EAGAIN or EWOULDBLOCK
110              The socket's file descriptor is marked O_NONBLOCK and no data is
111              waiting to be received; or MSG_OOB is  set  and  no  out-of-band
112              data  is  available  and  either the socket's file descriptor is
113              marked O_NONBLOCK or the socket does  not  support  blocking  to
114              await out-of-band data.
115
116       EBADF  The socket argument is not a valid file descriptor.
117
118       ECONNRESET
119              A connection was forcibly closed by a peer.
120
121       EINTR  A signal interrupted recvfrom() before any data was available.
122
123       EINVAL The MSG_OOB flag is set and no out-of-band data is available.
124
125       ENOTCONN
126              A  receive  is attempted on a connection-mode socket that is not
127              connected.
128
129       ENOTSOCK
130              The socket argument does not refer to a socket.
131
132       EOPNOTSUPP
133              The specified flags are not supported for this socket type.
134
135       ETIMEDOUT
136              The connection timed out during connection establishment, or due
137              to a transmission timeout on active connection.
138
139       The recvfrom() function may fail if:
140
141       EIO    An  I/O error occurred while reading from or writing to the file
142              system.
143
144       ENOBUFS
145              Insufficient resources were available in the system  to  perform
146              the operation.
147
148       ENOMEM Insufficient memory was available to fulfill the request.
149
150       The following sections are informative.
151

EXAMPLES

153       None.
154

APPLICATION USAGE

156       The select() and poll() functions can be used to determine when data is
157       available to be received.
158

RATIONALE

160       None.
161

FUTURE DIRECTIONS

163       None.
164

SEE ALSO

166       poll(),  pselect(),  read(),  recv(),  recvmsg(),  send(),   sendmsg(),
167       sendto(), shutdown(), socket(), write()
168
169       The Base Definitions volume of POSIX.1‐2017, <sys_socket.h>
170
172       Portions  of  this text are reprinted and reproduced in electronic form
173       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
174       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
175       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
176       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
177       event of any discrepancy between this version and the original IEEE and
178       The  Open Group Standard, the original IEEE and The Open Group Standard
179       is the referee document. The original Standard can be  obtained  online
180       at http://www.opengroup.org/unix/online.html .
181
182       Any  typographical  or  formatting  errors that appear in this page are
183       most likely to have been introduced during the conversion of the source
184       files  to  man page format. To report such errors, see https://www.ker
185       nel.org/doc/man-pages/reporting_bugs.html .
186
187
188
189IEEE/The Open Group                  2017                         RECVFROM(3P)
Impressum