recvfrom

Receives bytes from a socket from any source.

Format

#include  <socket.h>

int recvfrom  (int s, char *buf, int len,
              int flags, struct sockaddr *from,
              int *fromlen) ;
Routine Variants This socket routine has a variant named __bsd44_recvfrom. Enabled by defining _SOCKADDR_LEN, this variant implements 4.4BSD-compatible semantics. See Section A.7 for more information.

Arguments

s
A socket descriptor that has been created with socket and bound to a name using bind or as a result of accept.
buf
A pointer to a buffer into which received data will be placed.
len
The size of the buffer pointed to by buf.
flags
A bit mask that may contain one or more of MSG_OOB and MSG_ PEEK. It is built by ORing the appropriate values together.

The MSG_OOB flag allows out-of-band data to be received. If out-of- band data is available, it will be read before any other data that is available. If no out-of-band data is available, the MSG_OOB flag is ignored. Out-of-band data can be sent using send, sendmsg, and sendto.

The MSG_PEEK flag allows you to peek at the data that is next in line to be received without actually removing it from the system's buffers.

from
If from is nonzero, from is a buffer into which recvfrom places the address (structure) of the socket from which the data is received. If from was 0, the address will not be returned.
fromlen
Points to an integer containing the size of the buffer pointed to by from. On return, the integer is modified to contain the actual length of the socket address structure returned.

Description

This routine allows a named, unconnected socket to receive data. The data is placed in the buffer pointed to by buf, and the address of the sender of the data is placed in the buffer pointed to by from if from is non-NULL. The structure that from points to is assumed to be as large as the sockaddr structure. See <socket.h> for a description of the sockaddr structure.

To receive bytes from any source, the sockets need not be connected to another socket.

You may use the select routine to determine if data is available.

If no data is available at the socket, the recv call waits for data to arrive, unless the socket is nonblocking, in which case a -1 is returned with the external variable errno set to EWOULDBLOCK.

See also read, send, sendmsg, sendto, and socket in this section.

Return Values
The number of bytes of data received and placed in buf
-1  Indicates an error; errno is set to one of the following values:

  • EBADF - The socket descriptor is invalid.

  • ENOTSOCK - The socket descriptor references a file, not a socket.

  • EPIPE - An attempt was made to write to a socket that is not open for reading by any process.

  • EWOULDBLOCK - The NBIO (nonblocking) flag is set for the socket descriptor and the process would be delayed in the write operation.

  • EFAULT - The data was specified to be received into a non-existent or protected part of the process address space.
 


Previous Page | Next Page | Table of Contents | Index