recvfrom: Receive data on connected/unconnected socket
The recvfrom socket function receives data on a socket with descriptor s and stores it in a buffer.
Last updated
- Changed for PUT15 (information only; no code change).
- Changed for PUT14.
- Added for PUT00.
Format
#include <sys/socket.h>
int recvfrom(int s,
char *buf,
int len,
int flags,
struct sockaddr *name,
int *namelen); - s
- The socket descriptor.
- buf
- A pointer to the buffer that receives the data.
- len
- The length, in bytes, of the buffer pointed to by the buf parameter. The maximum amount of data that can be received is 1048576 bytes.
- flags
- Must be set to 0 or one or more of the following flags. If you
specify more than one flag, use the logical OR operator (|) to separate
them:
- MSG_OOB
- Reads any out-of-band data on the socket.
- MSG_PEEK
- Peeks at the data present on the socket; the data is returned but not consumed so a later receive operation sees the same data.
- name
- A pointer to a socket address that data is received from.
- namelen
- A pointer to the size of name in bytes.
Normal return
If successful, the function returns the length, in bytes, of
the message or datagram:
- For UDP sockets, zero length messages can be received on the socket.
- For TCP sockets, if the connection is closed, 0 is returned.
Error return
A return code equal to -1 indicates an error. You can get the specific error code by calling sock_errno. For more information about socket errors, see
Socket error return codes.
- E1052STATE
- The socket was closed because the system was in or cycling down to 1052 state.
- ESYSTEMERROR
- A system error has occurred and closed the socket.
- SOCFAULT
- Using buf and len would result in an attempt to access a protected address space.
- SOCINVAL
- The value of the namelen parameter is not valid, the MSG_OOB option was specified for a socket that is not a stream socket, or the MSG_OOB option was specified, but out-of-band data is queued inline for this socket.
- SOCNETUNREACH
- An ICMP message was received on this socket (in response to sent data) indicating that the destination IP address and/or destination port are not reachable.
- SOCNOTCONN
- A stream socket was used to issue the recvfrom function, and the socket was not connected.
- SOCNOTSOCK
- The s parameter is not a valid socket descriptor.
- SOCTIMEDOUT
- The operation timed out. The socket is still available.
- SOCWOULDBLOCK
- The s parameter is in nonblocking mode and either no data is available to read or the traffic limit has been reached for the socket or application.
Programming considerations
- The recvfrom function receives data on a socket with descriptor s and stores it in the caller's buffer.
- If the name is nonzero, the source address of the message is returned. The namelen parameter is first initialized by the caller to the size of the buffer associated with name; on return, it is modified to indicate the actual number of bytes stored there.
- The recvfrom function returns the length of the incoming message or data. If a message is too long to fit in the supplied buffer, the message is truncated. If a message is not available at the socket with descriptor s, the recvfrom function waits for a message to arrive and blocks the caller unless the socket is in nonblocking mode. See ioctl: Perform special operations on socket for a description of how to set nonblocking mode.
- If a bind has not yet been issued to the socket, a bind is issued on behalf of the application for a non-stream socket.
- The receive timeout value (the SO_RCVTIMEO setsockopt option) determines how long to wait for data to be received before the recvfrom function times out.
- The receive low-water mark (the SO_RCVLOWAT setsockopt option) determines the minimum amount of data that must be received before the recvfrom function is competed. If the recvfrom function times out, any data that was received is returned to the application even if the amount of data received is less than the receive low-water mark value.
- The recvfrom function cannot be issued if an activate_on_receipt, activate_on_receipt_with_length, activate_on_receipt_of_TCP_message, or tpf_read_TCP_message call is pending for the socket. These operations are mutually exclusive.
Examples
In the following example, the
application issues a recvfrom to
receive a message but does not request the address of the source of
the message.
int bytes_recv;
int server_sock;
char data_recv[256];
⋮
bytes_recv = recvfrom(server_sock, data_recv, sizeof(data_recv), 0,
(struct sockaddr *) 0, (int *) 0);