recvfrom()
PROTOTYPE
#include <sockets.h>
ssize_t recvfrom(int socket, void *buffer, size_t buflen, int flags, void *from, socklen_t *fromlen);
DESCRIPTION
For TCP sockets, recvfrom() behaves exactly like recv(). The from and fromlen arguments are ignored.
For UDP sockets, recvfrom() behaves like recv() except if from is not NULL. In that case, on successful return an address structure containing the connection’s remote address is written to *from, either type sockaddr_in or sockaddr_in6 according to the connection type. The IP address and port number are written in network byte order.
On input, *fromlen is the size of the buffer referenced by from. As output, it is the actual structure size, either sizeof(struct sockaddr_in) or sizeof(struct sockaddr_in6). If the buffer size is less than the structure size, the output is truncated to fit the buffer size.
Refer to the description of recv() for more information.
If successful, recvfrom() returns the number of bytes received. Otherwise, it sets errno and returns -1.
ERROR CODES
| ECONNABORTED | Connection was aborted because of unacknowledged transmissions. |
| ECONNRESET | Connection was forcibly closed by remote host (received RST). |
| EFAULT | Either buflen is not 0 and buffer is NULL, or from is not NULL and either fromlen is NULL or *fromlen is less than 0. |
| EHOSTUNREACH | Timed out after receiving a “host unreachable” ICMP message. |
| EINVAL | MSG_OOB is set and either SO_OODINLINE is set or no urgent data is available, UDP socket is not bound to a local port number, or the flags combination is illegal. |
| EMSGSIZE | Datagram size is larger than buflen and was truncated (UDP only). |
| ENETDOWN | TargetTCP has not been initialized. |
| ENOTCONN | The socket is unconnected (TCP only). |
| ENOTSOCK | socket is not a valid socket handle. |
| EOPNOTSUPP | MSG_OOB or MSG_WAITALL specified for UDP socket. |
| ETIMEDOUT | Timeout expired before call completed (connection not dropped). |
| EWOULDBLOCK | The socket is non-blocking and no data is currently available to receive. |
EXAMPLE
struct sockaddr_in from;
char echo_buf[UDP_BUF_SIZE];
int sock;
ssize_t length;
socklen_t addrlen;
...
/*-------------------------------------------------------------------*/
/* Wait for a received datagram. */
/*-------------------------------------------------------------------*/
addrlen = sizeof(struct sockaddr_in);
length = recvfrom(sock, echo_buf, UDP_BUF_SIZE, 0, &from, &addrlen);
if (length < 0)
perror("recvfrom() failed");