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. |
This function receives data on a socket.
Syntax
int WSPRecv( SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount, LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine, LPWSATHREADID lpThreadId, LPINT lpErrno ); |
Parameters
- s
-
[in] Descriptor identifying a connected 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.
- lpFlags
-
[in, out] Pointer to flags.
- lpOverlapped
-
[in] Pointer to a WSAOVERLAPPEDstructure (ignored for nonoverlapped structures).
- lpCompletionRoutine
-
[in] Pointer to the completion routine called when the receive operation has been completed (ignored for nonoverlapped structures).
- lpThreadId
-
[in] Pointer to a WSATHREADIDstructure to be used by the provider to determine the thread to run.
- lpErrno
-
[out] Pointer to the error code.
Return Value
If no error occurs and the receive operation has completed immediately, this function returns zero. Note that in this case the completion routine, if specified, will have already been queued. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code is available in lpErrno. 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 no overlapped operations were initiated and no completion indication will occur.
The following table shows the possible error codes.
Error value | Description |
---|---|
WSAENETDOWN |
Network subsystem has failed. |
WSAENOTCONN |
Socket is not connected. |
WSAEINPROGRESS |
Blocking Windows Sockets call is in progress, or the service provider is still processing a callback function. |
WSAENETRESET |
Connection has been broken due to keep-alive activity detecting a failure while the operation was in progress. |
WSAEFAULT |
The lpBuffersparameter is not totally contained in a valid part of the user address space. |
WSAENOTSOCK |
Descriptor is not a socket. |
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 |
Socket has been shut down; it is not possible to receive through WSPRecvon a socket after WSPShutdownhas been invoked with howset to SD_RECEIVE or SD_BOTH. |
WSAEWOULDBLOCK |
Overlapped sockets: there are too many outstanding overlapped I/O requests. Nonoverlapped sockets: The socket is marked as nonblocking and the receive operation cannot be completed immediately. |
WSAEMSGSIZE |
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. |
WSAEINVAL |
Socket has not been bound (for example, with WSPBind) or the socket is not created with the overlapped flag. |
WSAECONNABORTED |
Virtual circuit was terminated due to a time-out or other failure. |
WSAECONNRESET |
Virtual circuit was reset by the remote side. |
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 |
Overlapped operation has been canceled due to the closure of the socket. |
Remarks
This function is used on connected sockets or bound connectionless sockets specified by the sparameter and is used to read incoming data. The socket's local address must be known. This may be done explicitly through WSPBindor implicitly through WSPAccept, WSPConnect, WSPSendTo, or WSPJoinLeaf.
For connected, connectionless sockets, this function restricts the addresses from which received messages are accepted. The function only returns messages from the remote address specified in the connection. Messages from other addresses are (silently) discarded.
For overlapped sockets, this function is used to post one or more buffers into which incoming data will be placed as it becomes available, after which the Windows Sockets SPI client-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 WSPGetOverlappedResult.
If both lpOverlappedand lpCompletionRoutineare NULL, the socket in this function will be treated as a nonoverlapped socket.
For nonoverlapped sockets, the lpOverlapped, lpCompletionRoutine, and lpThreadIdparameters 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. Windows Sockets 2 does not define any standard blocking timeout mechanism for this function. For protocols acting as byte-stream protocols the stack tries to return as much data as possible subject to the supplied buffer space and amount of received data available. However, receipt of a single byte is sufficient to unblock the caller. There is no guarantee that more than a single byte will be returned. For protocols acting as message-oriented, a full message is required to unblock the caller.
Whether or not a protocol is acting as byte-stream is determined by the setting of XP1_MESSAGE_ORIENTED and XP1_PSEUDO_STREAM in its WSAPROTOCOL_INFO structure and the setting of the MSG_PARTIAL flag passed in to this function (for protocols that support it). The following table shows the relevant combinations; an asterisk (*) indicates that the setting of this bit does not matter in this case.
XP1_MESSAGE _ORIENTED | XP1_PSEUDO _STREAM | MSG_PARTIAL | Acts as |
---|---|---|---|
not set |
* |
* |
byte-stream |
* |
set |
* |
byte-stream |
set |
not set |
set |
byte-stream |
set |
not set |
not set |
message-oriented |
The supplied buffers are filled in the order in which they appear in the array pointed to 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 this array of pointers to WSABUFstructures before returning from this call. This enables Windows Sockets SPI clients to build stack-based WSABUFarrays.
For byte stream-style sockets (for example, type SOCK_STREAM), incoming data is placed into the buffers until the buffers are filled, the connection is closed, or 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 (for example, type SOCK_DGRAM), 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 service provider, the MSG_PARTIAL flag is set in lpflagsand subsequent receive operation(s) can be used to retrieve the rest of the message. If MSG_PARTIAL is not supported but the protocol is reliable, WSPRecvgenerates 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 WSPRecvgenerates the error WSAEMSGSIZE.
For connection-oriented sockets, WSPRecvcan indicate the graceful termination of the virtual circuit in one of two ways, depending on whether the socket is a byte stream or message oriented. For byte streams, zero bytes having been read indicates graceful closure and that no more bytes will ever be read. For message-oriented sockets, where a zero byte message is often allowable, a return error code of WSAEDISCON is used to indicate graceful closure. In any case a return error code of WSAECONNRESET indicates an abortive close has occurred.
lpFlagscan 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 latter is constructed by using the bitwise OR operator with any of the following values.
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 OOB data. |
MSG_PARTIAL |
This flag is for message-oriented sockets only. On output, 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 MSG_PARTIAL flag cleared indicates end of sender's message. As an input parameter, MSG_PARTIAL indicates that the receive operation should complete even if only part of a message has been received by the service provider. |
Overlapped Socket I/O
If an overlapped operation completes immediately, WSPRecvreturns 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, this function returns SOCKET_ERROR and indicates error code WSA_IO_PENDING. In this case, lpNumberOfBytesRecvdand lpFlagsare 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 WSPGetOverlappedResult. Flag values are obtained either through the dwFlagsparameter of the completion routine, or by examining the lpdwFlagsparameter of WSPGetOverlappedResult.
Providers must allow this function to be called from within the completion routine of a previous WSPRecv, WSPRecvFrom, WSPSend, or WSPSendTofunction. However, for a given socket, I/O completion routines cannot be nested. This permits time-sensitive data transmissions to occur entirely within a preemptive 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 overlapped structure. The following code sample shows the WSAOVERLAPPEDstructure format.
Copy Code | |
---|---|
typedef struct _WSAOVERLAPPED { DWORD Internal; // reserved DWORD InternalHigh; // reserved DWORD Offset; // reserved DWORD OffsetHigh; // reserved WSAEVENT hEvent; } WSAOVERLAPPED, FAR* LPWSAOVERLAPPED; |
If the lpCompletionRoutineparameter is NULL, the service provider signals the hEventmember of lpOverlappedwhen the overlapped operation completes if it contains a valid event object handle. The Windows Sockets SPI client can use WSPGetOverlappedResultto wait or poll on the event object.
If lpCompletionRoutineis not NULL, the hEventmember is ignored and can be used by the Windows Sockets SPI client to pass context information to the completion routine. A client 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 hEventmember is undefined, and attempting to wait on the hEventmember would produce unpredictable results.
It is the service provider's responsibility to arrange for invocation of the client specified–completion routine when the overlapped operation completes. Because the completion routine must be executed in the context of the same thread that initiated the overlapped operation, it cannot be invoked directly from the service provider.
The following code sample shows the prototype for the client-supplied completion routine.
Copy Code | |
---|---|
void CALLBACK CompletionRoutine ( IN DWORD dwError, IN DWORD cbTransferred, IN LPWSAOVERLAPPED lpOverlapped, IN DWORD dwFlags ); |
CompletionRoutineis a placeholder for a client-supplied function name. dwErrorspecifies the completion status for the overlapped operation as indicated by lpOverlapped. cbTransferredspecifies the number of bytes received. dwFlagscontains information that would have appeared in lpFlagsif the receive operation had completed immediately. This function does not return a value.
The completion routines can be called in any order, but not necessarily the same order in which the overlapped operations are completed. However, the posted buffers are guaranteed to be filled in the same order in which they are supplied.
Requirements
Header | ws2spi.h |
Library | Ws2.lib |
Windows Embedded CE | Windows CE .NET 4.0 and later |
Windows Mobile | Windows Mobile Version 5.0 and later |