recvfrom Subroutine

Purpose

Receives messages from sockets.

Library

Standard C Library (libc.a)

Syntax

#include <sys/socket.h>

ssize_t recvfrom
(Socket, Buffer, Length, Flags, From, FromLength)
int  Socket;
void * Buffer;
size_t  Length,
int  Flags;
struct sockaddr * From;
socklen_t * FromLength;

Description

The recvfrom subroutine allows an application program to receive messages from unconnected sockets. The recvfrom subroutine is normally applied to unconnected sockets as it includes parameters that allow the calling program to specify the source point of the data to be received.

To return the source address of the message, specify a nonnull value for the From parameter. The FromLength parameter is a value-result parameter, initialized to the size of the buffer associated with the From parameter. On return, the recvfrom subroutine modifies the FromLength parameter to indicate the actual size of the stored address. The recvfrom subroutine returns the length of the message. If a message is too long to fit in the supplied buffer, excess bytes may be truncated depending on the type of socket that issued the message.

If no messages are available at the socket, the recvfrom subroutine waits for a message to arrive, unless the socket is nonblocking. If the socket is nonblocking, the system returns an error.

The socket applications can be compiled with COMPAT_43 defined. This will make the sockaddr structure BSD 4.3 compatible. For more details refer to socket.h.

Parameters

Item Description
Socket Specifies the socket descriptor.
Buffer Specifies an address where the message should be placed.
Length Specifies the size of the Buffer parameter.
Flags Points to a value controlling the message reception. The argument to receive a call is formed by logically ORing one or more of the values shown in the following list:
MSG_OOB
Processes out-of-band data. The significance of out-of-band data is protocol-dependent.
MSG_PEEK
Peeks at incoming data. The data continues to be treated as unread and will be read by the next call to recv() or a similar function.
MSG_WAITALL
Requests that the function not return until the requested number of bytes have been read. The function can return fewer than the requested number of bytes only if a signal is caught, the connection is terminated, or an error is pending for the socket.
From Points to a socket structure, filled in with the source's address.
FromLength Specifies the length of the sender's or source's address.

Return Values

If the recvfrom subroutine is successful, the subroutine returns the length of the message in bytes.

If the call is unsuccessful, the subroutine handler performs the following functions:

Error Codes

The recvfrom subroutine is unsuccessful if any of the following errors occurs:

Error Description
EBADF The Socket parameter is not valid.
ECONNRESET The remote peer forces the connection to be closed.
EFAULT The data was directed to be received into a nonexistent or protected part of the process address space. The buffer is not valid.
EINTR The receive is interrupted by a signal delivery before any data is available.
EINVAL The MSG_OOB flag is set but no out-of-band data is available.
ENOBUF Insufficient resources are available in the system to perform the operation.
ENOPROTOOPT The protocol is not 64-bit supported.
ENOTCONN A receive is attempted on a SOCK_STREAM socket that is not connected.
ENOTSOCK The Socket parameter refers to a file, not a socket.
EOPNOTSUPP MSG_OOB flag is set for a SOCK_DGRAM socket, or MSG_OOB flag is set for any AF_UNIX socket.
ETIMEDOUT The connection timed out during connection establishment, or there was a transmission timeout on an active connection.
EWOULDBLOCK The socket is marked nonblocking, and no connections are present to be accepted.