CAsyncSocket::ReceiveFrom

int ReceiveFrom( void* lpBuf, int nBufLen, CString& rSocketAddress, UINT& rSocketPort, int nFlags = 0 );

int ReceiveFrom( void* lpBuf, int nBufLen, SOCKADDR* lpSockAddr, int* lpSockAddrLen, int nFlags = 0 );

Return Value

If no error occurs, ReceiveFrom returns the number of bytes received. If the connection has been closed, it returns 0. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling GetLastError. The following errors apply to this member function:

Parameters

lpBuf

A buffer for the incoming data.

nBufLen

The length of lpBuf in bytes.

rSocketAddress

Reference to a CString object that receives a dotted number IP address.

rSocketPort

Reference to a UINT that stores a port.

lpSockAddr

A pointer to a SOCKADDR structure that holds the source address upon return.

lpSockAddrLen

A pointer to the length of the source address in lpSockAddr in bytes.

nFlags

Specifies the way in which the call is made. The semantics of this function are determined by the socket options and the nFlags parameter. The latter is constructed by combining any of the following values with the C++ OR operator:

Remarks

Call this member function to receive a datagram and store the source address in the SOCKADDR structure or in rSocketAddress. This function is used to read incoming data on a (possibly connected) socket and capture the address from which the data was sent.

For sockets of type SOCK_STREAM, as much information as is currently available up to the size of the buffer supplied is returned. If the socket has been configured for in-line reception of out-of-band data (socket option SO_OOBINLINE) and out-of-band data is unread, only out-of-band data will be returned. The application can use the IOCtl SIOCATMARK option or OnOutOfBandData to determine whether any more out-of-band data remains to be read. The lpSockAddr and lpSockAddrLen parameters are ignored for SOCK_STREAM sockets.

For datagram sockets, data is extracted from the first enqueued datagram, up to the size of the buffer supplied. If the datagram is larger than the buffer supplied, the buffer is filled with the first part of the message, the excess data is lost, and ReceiveFrom returns a value of SOCKET_ERROR with the error code set to WSAEMSGSIZE.

If lpSockAddr is nonzero, and the socket is of type SOCK_DGRAM, the network address of the socket which sent the data is copied to the corresponding SOCKADDR structure. The value pointed to by lpSockAddrLen is initialized to the size of this structure, and is modified on return to indicate the actual size of the address stored there. If no incoming data is available at the socket, the ReceiveFrom call waits for data to arrive unless the socket is nonblocking. In this case, a value of SOCKET_ERROR is returned with the error code set to WSAEWOULDBLOCK. The OnReceive callback can be used to determine when more data arrives.

If the socket is of type SOCK_STREAM and the remote side has shut down the connection gracefully, a ReceiveFrom will complete immediately with 0 bytes received.

CAsyncSocket OverviewClass MembersHierarchy Chart

See Also   CAsyncSocket::AsyncSelect, CAsyncSocket::Create, CAsyncSocket::Receive, CAsyncSocket::Send