z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


recvfrom()

z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
SC27-3660-00

The recvfrom() call receives data on a socket by name with descriptor s and stores it in a buffer. The recvfrom() call applies to any datagram socket, whether connected or unconnected. For a datagram socket, when name is nonzero, the source address of the message is filled. Parameter namelen must first be initialized to the size of the buffer associated with name; then it is modified on return to indicate the actual size of the address stored there.

This call returns the length of the incoming message or data. If a datagram packet is too long to fit in the supplied buffer, datagram sockets discard extra bytes. If data is not available for the socket s, and s is in blocking mode, the recvfrom() call blocks the caller until data arrives. If data is not available, and s is in nonblocking mode, recvfrom() returns a -1 and sets errno to EWOULDBLOCK. See fcntl() or ioctl() to set nonblocking mode.

For datagram sockets, this call returns the entire datagram sent, providing the datagram can fit into the specified buffer. Stream sockets act like streams of information with no boundaries separating data. For example, if applications A and B are connected with a stream socket, and Application A sends 1000 bytes, each call to this function can return 1 byte, or 10 bytes, or the entire 1000 bytes. Therefore, applications using stream sockets should place this call in a loop, calling this function until all data has been received.

For datagram protocols, recvfrom() returns the source address associated with each incoming datagram. For connection-oriented protocols like TCP, getpeername() returns the address associated with the remote end of the connection.

#include <manifest.h>
#include <bsdtypes.h>
#include <socket.h>
 
int recvfrom(int s, char *buf, int len, int flags,
struct sockaddr *name, int *namelen)
Parameter
Description
s
Socket descriptor.
buf
Pointer to the buffer to receive the data.
len
Length in bytes of the buffer pointed to by buf.
flags
A parameter that can be set to 0 or MSG_PEEK, or MSG_OOB. Setting this parameter is supported only for sockets in the AF_INET domain. Setting these flags is not supported for sockets in the AF_IUCV domain.
MSG_OOB
Reads any out-of-band data on the socket. This is valid for stream (TCP) sockets only.
MSG_PEEK
Peeks at the data present on the socket; the data is returned but not consumed, so that a subsequent receive operation sees the same data.
name
Points to a socket address structure from which data is received. If name is a nonzero value, the source address is returned (datagram sockets).
namelen
Points to the size of name in bytes.

Return values

If successful, the length of the message or datagram is returned in bytes. The value 0 indicates that the connection is closed. The value -1 indicates an error. Errno identifies the specific error.
Errno
Description
EBADF
Indicates that s is not a valid socket descriptor.
EFAULT
Using the buf and len parameters would result in an attempt to access storage outside the caller address space.
EWOULDBLOCK
Indicates that s is in nonblocking mode and data is not available to read.
ENOTCONN
Indicates an unconnected TCP socket.
EMSGSIZE
For non-TCP sockets, this indicates that length exceeds the maximum data size as determined by getsockopt() using SO_SNDBUF for the socket type, either TCP, UDP, or RAW.
EINVAL
Parameter namelen is not valid.

Related calls

fcntl(), getsockopt(), ioctl(), read(), readv(), recv(), recvmsg(), select(), selectex(), send(), sendmsg(), sendto(), setsockopt(), socket(), write(), writev()

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014