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


z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference

The recvmsg() call receives messages on the socket with descriptor s and stores the messages in an array of message headers.

For datagram protocols, recvmsg() 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 recvmsg(int s, struct msghdr *msg, int flags)
Socket descriptor.
Points to an msghdr structure.
Set the flags parameter by specifying one or more of the following flags. If more than one flag is specified, the logical OR operator ( | ) must be used to separate them. 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.
Reads any out-of-band data on the socket. This is valid for stream (TCP) sockets only.
Peeks at the data present on the socket; the data is returned but not consumed, so that a subsequent receive operation will see the same data.
A message header is defined by structure msghdr. The definition of this structure can be found in the SOCKET.H header file and contains the following elements:
An optional pointer to a buffer where the sender address is stored for datagram sockets.
The size of the address buffer.
An array of iovec buffers into which the message is placed. An iovec buffer contains the following variables:
Points to the buffer.
The length of the buffer.
The number of elements in the msg_iov array.
The access rights received. This field is ignored.
The length of access rights received. This field is ignored.
The recvmsg() call applies to sockets, regardless of whether they are in the connected state, except for TCP sockets, which must be connected.

This call returns the length of the data received. If data is not available for socket s, and s is in blocking mode, the recvmsg() call blocks the caller until data arrives. If data is not available, and s is in nonblocking mode, recvmsg() returns a -1 and sets errno to EWOULDBLOCK. See fcntl() or ioctl() to see how to set nonblocking mode.

If a datagram packet is too long to fit in the supplied buffer, datagram sockets discard extra bytes. 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, and call this function until all data has been received.

Return values

If successful, the length of the message in bytes is returned. The value 0 indicates that the connection is closed. The value -1 indicates an error. Errno identifies the specific error.
Indicates that s is not a valid socket descriptor.
Using msg would result in an attempt to access storage outside the caller address space. Also returned when msg_namelen is not valid.
Indicates that s is in nonblocking mode and data is not available to read.
Returned for an unconnected TCP socket.
For non-TCP sockets, this indicates that length exceeds the maximum data size determined by getsockopt() using SO_SNDBUF for the socket type (TCP, UDP, or RAW).

Related calls

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

Go to the previous page Go to the next page

Copyright IBM Corporation 1990, 2014