Receives a message from any socket.
Standard C Library (libc.a)
The recvmsg subroutine receives messages from unconnected or connected sockets. The recvmsg 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 recvmsg subroutine waits for a message to arrive. If the socket is nonblocking and no messages are available, the recvmsg subroutine is unsuccessful.
Use the select subroutine to determine when more data arrives.
The recvmsg subroutine uses a msghdr structure to decrease the number of directly supplied parameters. The msghdr structure is defined in thesys/socket.h file. In BSD 4.3 Reno, the size and members of the msghdr structure have been modified. Applications wanting to start the old structure need to compile with COMPAT_43 defined. The default behavior is that of BSD 4.4.
All applications containing the recvmsg subroutine must be compiled with the _BSD macro set to a specific value. Acceptable values are 43 and 44. In addition, all socket applications must include the BSD libbsd.a library.
Item | Description |
---|---|
Socket | Specifies the unique name of the socket. |
Message | Points to the address of the msghdr structure, which contains both the address for the incoming message and the space for the sender address. |
Flags | Permits the subroutine to exercise control over the reception
of messages. The Flags parameter used to receive a call is
formed by logically ORing one or more of the values shown in the following
list:
The /sys/socket.h file contains the possible values for the Flags parameter. |
Upon successful completion, the length of the message in bytes is returned.
If the recvmsg subroutine is unsuccessful, the subroutine handler performs the following functions:
The recvmsg subroutine is unsuccessful if any of the following error codes occurs:
Error | Description |
---|---|
EBADF | The Socket parameter is not valid. |
ECONNRESET | The remote peer forces the connection to be closed. |
EFAULT | The Address parameter is not in a writable part of the user address space. |
EINTR | The recvmsg subroutine was interrupted by delivery of a signal before any data was available for the receive. |
EINVAL | The length of the msghdr structure is invalid, or the MSG_OOB flag is set and no out-of-band data is available. |
EMSGSIZE | The msg_iovlen member of the msghdr structure pointed to by Message is less than or equal to 0, or is greater than IOV_MAX. |
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. |