recvfrom |
Prototype: |
#include <sys/socket.h>
#include <resolv.h>
int recvfrom(int sockfd, char *buf, int buf_len, int options,
struct sockaddr *addr, int *addr_len);
|
General Description: |
Wait for and receive a message from an unconnected peer (UDP and raw sockets). Please note that T/TCP never uses this call. Instead, the receiver uses accept() like usual. |
Return Value: |
If successful, the number of bytes read. If an error occurred, the return value is -1, and errno holds the error code. |
Parameters |
sockfd |
The open socket descriptor. |
buf |
The byte array to receive the data. |
buf_len |
The maximum size of the buffer (message truncated and dropped if buffer too small). |
options |
Channel controlling options (same as recv()). |
addr |
The sending peer's address and port. |
addr_len |
The maximum size of addr (address truncated if too small). Upon return, this value changes to match the number of bytes used. |
Possible Errors |
(same as recv()) |
|
|
Examples |
struct sockaddr_in addr;
int addr_len=sizeof(addr), bytes_read;
char buf[1024];
int sockfd = socket(PF_INET, SOCK_DGRAM, 0);
|
bytes_read = recvfrom(sockfd, buf, sizeof(buf), 0, &addr, &add_len);
if ( bytes_read < 0 )
perror("recvfrom failed");
|