Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

This function receives data on a socket and stores the source address.

Syntax

int WSARecvFrom(
  SOCKET 
s,
  LPWSABUF 
lpBuffers,
  DWORD 
dwBufferCount,
  LPDWORD 
lpNumberOfBytesRecvd,
  LPDWORD 
lpFlags,
  struct sockaddr FAR* 
lpFrom,
  LPINT 
lpFromlen,
  LPWSAOVERLAPPED 
lpOverlapped,
  LPWSAOVERLAPPED_COMPLETION_ROUTINE 
lpCompletionRoutine
);

Parameters

s

[in] Descriptor identifying a socket.

lpBuffers

[in, out] Pointer to an array of WSABUFstructures. Each WSABUFstructure contains a pointer to a buffer and the length of the buffer.

dwBufferCount

[in] Number of WSABUFstructures in the lpBuffersarray.

lpNumberOfBytesRecvd

[out] Pointer to the number of bytes received by this call if the receive operation completes immediately.

lpFlags

[in, out] Pointer to flags.

lpFrom

[out] Optional pointer to a buffer that will hold the source address on the completion of the overlapped operation.

lpFromlen

[in, out] Pointer to the size of the frombuffer, required only if lpFromis specified.

lpOverlapped

[in] Pointer to a WSAOVERLAPPEDstructure (ignored for nonoverlapped sockets).

lpCompletionRoutine

[in] Pointer to the completion routine called when the receive operation has been completed (ignored for nonoverlapped sockets).

Return Value

If no error occurs and the receive operation has completed immediately, this function returns zero. The overlapped structures are updated with the receive results, and the associated event is signalled.

In Windows Embedded CE, the completion object will not be signalled. If an error occurs, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling the WSAGetLastErrorfunction. The error code WSA_IO_PENDING indicates that the overlapped operation has been successfully initiated and that completion will be indicated at a later time. Any other error code indicates that the overlapped operation was not successfully initiated and no completion indication will occur. The following table shows a list of possible error codes.

Error code Description

WSANOTINITIALISED

A successful WSAStartupcall must occur before using this function.

WSAENETDOWN

The network subsystem has failed.

WSAEFAULT

The lpBuffers, lpFlags, lpFrom, lpNumberOfBytesRecvd, lpFromlen, lpOverlapped, or lpCompletionRoutineargument is not totally contained in a valid part of the user address space. The lpFrombuffer was too small to accommodate the peer address.

WSAEINTR

The socket was closed.

WSAEINPROGRESS

A blocking Winsock call is in progress, or the service provider is still processing a callback function.

WSAEINVAL

The socket has not been bound (with the bind (Windows Sockets)function, for example).

WSAENETRESET

The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress.

WSAENOTCONN

The socket is not connected (connection-oriented sockets only).

WSAEOPNOTSUPP

MSG_OOB was specified, but the socket is not stream style such as type SOCK_STREAM, OOB data is not supported in the communication domain associated with this socket, or the socket is unidirectional and supports only send operations.

WSAESHUTDOWN

The socket has been shut down. It is not possible to call WSARecvFromon a socket after shutdownhas been invoked with howset to SD_RECEIVE or SD_BOTH.

WSAEWOULDBLOCK

For overlapped sockets, there are too many outstanding overlapped I/O requests. For nonoverlapped sockets, the socket is marked as nonblocking and the receive operation cannot be completed immediately.

WSAEMSGSIZE

The message was too large to fit into the specified buffer and (for unreliable protocols only) any trailing portion of the message that did not fit into the buffer has been discarded.

WSAECONNRESET

The virtual circuit was reset by the remote side executing a hard or abortive close. The application should close the socket because it is no longer useable.

WSAEDISCON

Socket sis message-oriented and the virtual circuit was gracefully closed by the remote side.

WSA_IO_PENDING

An overlapped operation was successfully initiated and completion will be indicated at a later time.

WSA_OPERATION_ABORTED

The overlapped operation has been canceled due to the closure of the socket.

Remarks

This function provides functionality over and above the standard recvfromfunction in the following three important areas:

  • It can be used in conjunction with overlapped sockets to perform overlapped receive operations.

  • It allows multiple receive buffers to be specified, enabling a scatter/gather type of I/O.

  • The lpFlagsparameter is both an input and an output parameter, allowing applications to sense the output state of the MSG_PARTIAL flag bit. Note, however, that the MSG_PARTIAL flag bit is not supported by all protocols.

This function is used primarily on a connectionless socket specified by socket s. The socket's local address must be known. For server applications, this is usually done explicitly through bind. Explicit binding is discouraged for client applications. For client applications using this function, the socket can become bound implicitly to a local address through the sendtoor WSASendTofunction.

For overlapped sockets, this function is used to post one or more buffers into which incoming data will be placed as it becomes available on a possibly connected socket, after which the application-specified completion indication (invocation of the completion routine or setting of an event object) occurs. If the operation does not complete immediately, the final completion status is retrieved through the completion routine or the WSAGetOverlappedResultfunction. Also, the values indicated by lpFromand lpFromlenare not updated until completion is itself indicated. Applications must not use or disturb these values until they have been updated, therefore the application must not use automatic (that is, stack-based) variables for these parameters.

If both lpOverlappedand lpCompletionRoutineare NULL, the socket in this function will be treated as a nonoverlapped socket.

For nonoverlapped sockets, the blocking semantics are identical to that of the standard WSARecvfunction and the lpOverlappedand lpCompletionRoutineparameters are ignored. Any data that has already been received and buffered by the transport will be copied into the supplied user buffers. For the case of a blocking socket with no data currently having been received and buffered by the transport, the call will block until data is received.

The supplied buffers are completed in the order in which they appear in the array indicated by lpBuffers, and the buffers are packed so that no holes are created.

The array of WSABUFstructures pointed to by the lpBuffersparameter is transient. If this operation completes in an overlapped manner, it is the service provider's responsibility to capture these WSABUFstructures before returning from this call. This enables applications to build stack-based WSABUFarrays.

For connectionless socket types, the address from which the data originated is copied to the buffer indicated by lpFrom. The value pointed to by lpFromlenis initialized to the size of this buffer and is modified on completion to indicate the actual size of the address stored there. As noted previously for overlapped sockets, the lpFromand lpFromlenparameters are not updated until after the overlapped I/O has completed. The memory pointed to by these parameters must, therefore, remain available to the service provider and cannot be allocated on the application's stack frame. The lpFromand lpFromlenparameters are ignored for connection-oriented sockets.

For byte stream style sockets (for example, type SOCK_STREAM), incoming data is placed into the buffers until certain events occur. The following list shows these events:

  • The buffers are filled.

  • The connection is closed.

  • The internally buffered data is exhausted.

Regardless of whether or not the incoming data fills all the buffers, the completion indication occurs for overlapped sockets. For message-oriented sockets, an incoming message is placed into the supplied buffers up to the total size of the buffers supplied and the completion indication occurs for overlapped sockets. If the message is larger than the buffers supplied, the buffers are filled with the first part of the message. If MSG_PARTIAL is supported by the underlying service provider, the MSG_PARTIAL flag is set in lpFlagsand subsequent receive operations will retrieve the rest of the message. If MSG_PARTIAL is not supported but the protocol is reliable, WSARecvFromgenerates the error WSAEMSGSIZE and a subsequent receive operation with a larger buffer can be used to retrieve the entire message. Otherwise, (that is, the protocol is unreliable and does not support MSG_PARTIAL), the excess data is lost and WSARecvFromgenerates the error WSAEMSGSIZE.

The lpFlagsparameter can be used to influence the behavior of the function invocation beyond the options specified for the associated socket. That is, the semantics of this function are determined by the socket options and the lpFlagsparameter. The following table shows the values that are used with the bitwise OR operator to construct the lpFlagsparameter.

Value Description

MSG_PEEK

Peeks at the incoming data. The data is copied into the buffer but is not removed from the input queue. This flag is valid only for nonoverlapped sockets.

MSG_OOB

Processes out of band (OOB) data.

MSG_PARTIAL

This flag is for message-oriented sockets only. On output, this flag indicates that the data supplied is a portion of the message transmitted by the sender. Remaining portions of the message will be supplied in subsequent receive operations. A subsequent receive operation with the MSG_PARTIAL flag cleared indicates the end of the sender's message.

As an input parameter, this flag indicates that the receive operation should complete even if only part of a message has been received by the service provider.

For message-oriented sockets, the MSG_PARTIAL bit is set in the lpFlagsparameter if a partial message is received. If a complete message is received, MSG_PARTIAL is cleared in lpFlags. In the case of delayed completion, the value pointed to by lpFlagsis not updated. When completion has been indicated, the application should call WSAGetOverlappedResultand examine the flags pointed to by the lpdwFlagsparameter.

Overlapped Socket I/O

Note:
For Windows Embedded CE, avoid specifying completion routines for overlapped I/O operations. Because Windows Embedded CE does not support asynchronous procedure calls (APCs), which occur in the calling thread, the OS has to spin a thread for each call that specifies a completion routine. With one thread create per function call, using completion routines with overlapped I/O can quickly become very memory-consuming. Using events is recommended instead.

If an overlapped operation completes immediately, WSARecvFromreturns a value of zero and the lpNumberOfBytesRecvdparameter is updated with the number of bytes received and the flag bits pointed by the lpFlagsparameter are also updated. If the overlapped operation is successfully initiated and will complete later, WSARecvFromreturns SOCKET_ERROR and indicates error code WSA_IO_PENDING. In this case, lpNumberOfBytesRecvdand lpFlagsis not updated. When the overlapped operation completes, the amount of data transferred is indicated either through the cbTransferredparameter in the completion routine (if specified) or through the lpcbTransferparameter in WSAGetOverlappedResult. Flag values are obtained either through the dwFlagsparameter of the completion routine or by examining the lpdwFlagsparameter of WSAGetOverlappedResult.

The WSARecvFromfunction can be called from within the completion routine of a previous WSARecv, WSARecvFrom, WSASend, or WSASendTofunction call. For a given socket, I/O completion routines will not be nested. This permits time-sensitive data transmissions to occur entirely within a pre-emptive context.

The lpOverlappedparameter must be valid for the duration of the overlapped operation. If multiple I/O operations are simultaneously outstanding, each must reference a separate WSAOVERLAPPEDstructure.

If the lpCompletionRoutineparameter is NULL, the hEventparameter of lpOverlappedis signaled when the overlapped operation completes if it contains a valid event object handle. An application can use WSAGetOverlappedResultto wait or poll on the event object.

If lpCompletionRoutineis not NULL, the hEventparameter is ignored and can be used by the application to pass context information to the completion routine. A caller that passes a non-NULL lpCompletionRoutineand later calls WSAGetOverlappedResultfor the same overlapped I/O request may not set the fWaitparameter for that invocation of WSAGetOverlappedResultto TRUE. In this case, the usage of the hEventparameter is undefined and attempting to wait on the hEventparameter would produce unpredictable results.

The transport providers allow an application to invoke send and receive operations from within the context of the socket I/O completion routine and guarantee that, for a given socket, I/O completion routines will not be nested. This permits time-sensitive data transmissions to occur entirely within a pre-emptive context.

The following code sample shows the prototype of the completion routine.

Copy Code
void CALLBACK CompletionRoutine(
IN DWORD dwError, 
IN DWORD cbTransferred, 
IN LPWSAOVERLAPPED lpOverlapped, 
IN DWORD dwFlags
);

CompletionRoutineis a placeholder for an application-defined or library-defined function name. The dwErrorspecifies the completion status for the overlapped operation as indicated by lpOverlapped. The cbTransferredparameter specifies the number of bytes received. The dwFlagsparameter contains information that would have appeared in lpFlagsif the receive operation had completed immediately. This function does not return a value.

Returning from this function allows invocation of another pending completion routine for this socket.

Requirements

Header winsock2.h
Library Ws2.lib
Windows Embedded CE Windows CE .NET 4.0 and later
Windows Mobile Windows Mobile Version 5.0 and later