recvfrom()--Receive Data


Syntax
#include <sys/types.h>
#include <sys/socket.h>

int recvfrom(int socket_descriptor,
             char *buffer,
             int buffer_length,
             int flags,
             struct sockaddr *from_address,
             int *address_length)


Threadsafe: Yes

The recvfrom() function is used to receive data through a connected or unconnected socket.

Parameters

socket_descriptor
(Input) The socket descriptor that is to be read from.

buffer
(Input) The pointer to the buffer in which the data that is to be read is stored.

buffer_length
(Input) The length of the buffer.

int flags
(Input) A flag value that controls the reception of the data. The flags value is either zero, or is obtained by performing an OR operation on one or more of the following constants:
MSG_OOB
Receive out-of-band data. Valid only for sockets with an address family of AF_INET and type SOCK_STREAM.
MSG_PEEK
Obtain a copy of the message without removing the message from the socket.

from_address
(Output) A pointer to a buffer of type struct sockaddr that contains the address from which the message was received.

The structure sockaddr is defined in <sys/socket.h>.

      struct sockaddr {
         u_short sa_family;
         char    sa_data[14];
      };

The sa_family field identifies the address family to which the address belongs, and sa_data is the address whose format is dependent on the address family.

address_length
(Input/output) This parameter is a value-result field. The caller passes a pointer to the length of the from_address parameter. On return from the call, address_length will contain the actual length of the address.

Return Value

recvfrom() returns an integer. Possible values are:

Error Conditions

When recvfrom() fails, errno can be set to one of the following:

[EACCES]
Permission denied.

The socket pointed to by the socket_descriptor parameter is using a connection-oriented transport service, and a connect() was previously completed. The process, however, does not have the appropriate privileges to the objects that were needed to establish a connection. For example, the connect() required the use of an APPC device that the process was not authorized to.

[EBADF]
Descriptor not valid.
[ECONNABORTED]
Connection ended abnormally.

This error code indicates that the transport provider ended the connection abnormally because of one of the following:

[ECONNREFUSED]
The destination socket refused an attempted connect operation.
[ECONNRESET]
A connection with a remote socket was reset by that socket.
[EFAULT]
Bad address.

The system detected an address which was not valid while attempting to access the buffer, from_address, or address_length parameter.

[EINTR]
Interrupted function call.
[EINVAL]
Parameter not valid.

This error code indicates one of the following:

[EIO]
Input/output error.
[ENOBUFS]
There is not enough buffer space for the requested operation.
[ENOTCONN]
Requested operation requires a connection.

This error code is returned only on sockets that use a connection-oriented transport service.

[ENOTSOCK]
The specified descriptor does not reference a socket.
[EOPNOTSUPP]
Operation not supported.

This error code indicates one of the following:

[ETIMEDOUT]
A remote host did not respond within the timeout period.

A non-blocking connect() was previously issued that resulted in the connection establishment timing out. No connection is established. This error code is returned only on sockets that use a connection-oriented transport service.

[EUNATCH]
The protocol required to support the specified address family is not available at this time.
[EUNKNOWN]
Unknown system state.
[EWOULDBLOCK]
Operation would have caused the thread to be suspended.

Error Messages

CPE3418 E
Possible APAR condition or hardware failure.
CPF9872 E
Program or service program &1 in library &2 ended. Reason code &3.
CPFA081 E
Unable to set return value or error code.

Usage Notes

  1. For sockets that use a connection-oriented transport service (for example, sockets with a type of SOCK_STREAM), a returned value of zero indicates one of the following:

  2. If the socket is using a connection-oriented transport service, the from_address and address_length parameters are ignored.

  3. For sockets that have a type of SOCK_SEQPACKET, each output operation by the partner program constitutes one record. If the record length is larger than the value specified by the buffer_length parameter, the remaining data from the record is discarded.

  4. The following applies to sockets that use a connectionless transport service (for example, a socket with a type of SOCK_DGRAM):

Related Information


Top | Sockets APIs | APIs by category


[Information Center Home Page | Feedback ] [Legal | AS/400 Glossary]