recv.2 (2010 09)
r
recv(2) recv(2)
NAME
recv(), recvfrom(), recvmsg() - receive a message from a socket
SYNOPSIS
#include <sys/socket.h>
int recv(int s, void *buf, int len, int flags);
int recvfrom(
int s,
void *buf,
int len,
int flags,
void *from,
int *fromlen
);
int recvmsg(int s, struct msghdr msg[], int flags);
UNIX 03 Only (X/Open Sockets)
ssize_t recv(int s, void *buf, size_t len, int flags);
ssize_t recvfrom(
int s,
void *__restrict buf,
size_t len,
int flags,
struct sockaddr *__restrict from,
socklen_t *__restrict fromlen
);
ssize_t recvmsg(int s, struct msghdr *msg, int flags);
Obsolescent UNIX 95 Only (X/Open Sockets)
ssize_t recvfrom(
int s,
void *buf,
size_t len,
int flags,
struct sockaddr *from,
size_t *fromlen
);
DESCRIPTION
The recv(), recvfrom(), and recvmsg() system calls are used to receive messages from a socket.
s is a socket descriptor from which messages are received.
buf is a pointer to the buffer into which the messages are placed.
len is the maximum number of bytes that can fit in the buffer referenced by buf.
If the socket uses connection-based communications, such as a SOCK_STREAM socket, these calls can
only be used after the connection has been established (see connect (2)). For connectionless sockets such
as SOCK_DGRAM, these calls can be used whether a connection has been specified or not.
recvfrom() operates in the same manner as recv() except that it is able to return the address of the
socket from which the message was sent. For connected datagram sockets, recvfrom() simply returns
the same address as getpeername() (see getpeername (2)). For stream sockets, recvfrom()
retrieves data in the same manner as recv(), but does not return the socket address of the sender. If
from is nonzero, the source address of the message is placed in the socket address structure pointed to by
from. fromlen is a value-result parameter, initialized to the size of the structure associated with from,
and modified on return to indicate the actual size of the address stored there. If the memory pointed to
by from is not large enough to contain the entire address, only the first fromlen bytes of the address are
returned.
For message-based sockets such as SOCK_DGRAM, the entire message must be read in a single opera-
tion. If a message is too long to fit in the supplied buffer, the excess bytes are discarded. For stream-
HP-UX 11i Version 3: September 2010 − 1 − Hewlett-Packard Company 1