The WSPSendTo function sends data to a specific destination using overlapped I/O.
int WSPSendTo( _In_ SOCKET s, _In_ LPWSABUF lpBuffers, _In_ DWORD dwBufferCount, _Out_ LPDWORD lpNumberOfBytesSent, _In_ DWORD dwFlags, _In_ const struct sockaddr *lpTo, _In_ int iTolen, _In_ LPWSAOVERLAPPED lpOverlapped, _In_ LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine, _In_ LPWSATHREADID lpThreadId, _Out_ LPINT lpErrno );
A descriptor identifying a socket.
A pointer to an array of WSABUF structures. Each WSABUF structure contains a pointer to a buffer and the length of the buffer, in bytes. For a Winsock application, once the WSPSendTo function is called, the system owns these buffers and the application may not access them. This array must remain valid for the duration of the send operation.
The number of WSABUF structures in the lpBuffers array.
A pointer to the number of bytes sent by this call.
A set of flags that specifies the way in which the call is made.
An optional pointer to the address of the target socket in the sockaddr structure.
The size, in bytes, of the address pointed to by the lpTo parameter.
A pointer to a WSAOVERLAPPED structure (ignored for nonoverlapped sockets).
A pointer to the completion routine called when the send operation has been completed (ignored for nonoverlapped sockets).
A pointer to a WSATHREADID structure to be used by the provider in a subsequent call to WPUQueueApc. The provider should store the referenced WSATHREADID structure (not the pointer to same) until after the WPUQueueApc function returns.
A pointer to the error code.
If no error occurs and the receive operation has completed immediately, WSPSendTo 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 operation was initiated and no completion indication will occur.
The network subsystem has failed.
Requested address is a broadcast address, but the appropriate flag was not set.
(Blocking) call was canceled through WSPCancelBlockingCall.
Blocking Windows Sockets call is in progress, or the service provider is still processing a callback function.
The lpBuffers or lpTo parameters are not part of the user address space, or the lpTo parameter is too small.
The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress.
Windows Sockets provider reports a buffer deadlock.
Socket is not connected (connection-oriented sockets only).
The descriptor is not a socket.
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, MSG_PARTIAL is not supported, or the socket is unidirectional and supports only receive operations.
Windows NT: Overlapped sockets: there are too many outstanding overlapped I/O requests.
Nonoverlapped sockets: The socket is marked as nonblocking and the send operation cannot be completed immediately.
Socket is message oriented, and the message is larger than the maximum supported by the underlying transport.
Socket has not been bound with WSPBind, or the socket is not created with the overlapped flag.
Virtual circuit was terminated due to a time-out or other failure.
Virtual circuit was reset by the remote side.
Remote address is not a valid address (for example, ADDR_ANY).
Addresses in the specified family cannot be used with this socket.
Destination address is required.
The network cannot be reached from this host at this time.
Overlapped operation has been canceled due to the closure of the socket, or the execution of the SIO_FLUSH command in WSPIoctl.
The WSPSendTo function is normally used on a connectionless socket specified by s to send a datagram contained in one or more buffers to a specific peer socket identified by the lpTo parameter. Even if the connectionless socket has been previously connected to a specific address with the connect function, lpTo overrides the destination address for that particular datagram only. On a connection-oriented socket, the lpTo and iToLen parameters are ignored; in this case the WSPSendTo function is equivalent to WSPSend.
For overlapped sockets (created using WSPSocket with flag WSA_FLAG_OVERLAPPED) this will occur using overlapped I/O, unless both lpOverlapped and lpCompletionRoutine are NULL in which case the socket is treated as a nonoverlapped socket. A completion indication will occur (invocation of the completion routine or setting of an event object) when the supplied buffer(s) have been consumed by the transport. If the operation does not complete immediately, the final completion status is retrieved through the completion routine or WSPGetOverlappedResult.
For nonoverlapped sockets, the parameters lpOverlapped, lpCompletionRoutine, and lpThreadId are ignored and WSPSendTo adopts the regular synchronous semantics. Data is copied from the supplied buffer(s) into the transport's buffer. If the socket is nonblocking and stream oriented, and there is not sufficient space in the transport's buffer, WSPSendTo will return with only part of the Windows Sockets SPI client's buffers having been consumed. Given the same buffer situation and a blocking socket, WSPSendTo will block until all of the Windows Sockets SPI client's buffer contents have been consumed.
The array of WSABUF structures pointed to by the lpBuffers parameter is transient. If this operation completes in an overlapped manner, it is the service provider's responsibility to capture these WSABUF structures before returning from this call. This enables applications to build stack-based WSABUF arrays.
For message-oriented sockets, care must be taken not to exceed the maximum message size of the underlying transport, which can be obtained by getting the value of socket option SO_MAX_MSG_SIZE. If the data is too long to pass atomically through the underlying protocol the error WSAEMSGSIZE is returned, and no data is transmitted.
Note that the successful completion of a WSPSendTo does not indicate that the data was successfully delivered.
The iFlags parameter 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 dwFlags parameter. The latter is constructed by using the bitwise OR operator with any of the following values.
|MSG_DONTROUTE||Specifies that the data should not be subject to routing. A Windows Sockets service provider can choose to ignore this flag.|
|MSG_OOB||Sends OOB data (stream-style socket such as SOCK_STREAM only).|
|MSG_PARTIAL||Specifies that lpBuffers only contains a partial message. Note that the error code WSAEOPNOTSUPP will be returned by transports that do not support partial message transmissions.|
If an overlapped operation completes immediately, WSPSendTo returns a value of zero and the lpNumberOfBytesSent parameter is updated with the number of bytes sent. If the overlapped operation is successfully initiated and will complete later, WSPSendTo returns SOCKET_ERROR and indicates error code WSA_IO_PENDING. In this case, lpNumberOfBytesSent is not updated. When the overlapped operation completes the amount of data transferred is indicated either through the cbTransferred parameter in the completion routine (if specified), or through the lpcbTransfer parameter in WSPGetOverlappedResult.
Providers must allow this function to be called from within the completion routine of a previous WSPRecv, WSPRecvFrom, WSPSend or WSPSendTo function. 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 lpOverlapped parameter 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 WSAOVERLAPPED structure is defined in its own reference page.
If the lpCompletionRoutine parameter is null, the service provider signals the hEvent member of lpOverlapped when the overlapped operation completes if it contains a valid event object handle. Windows Sockets SPI clients can use WSPGetOverlappedResult to wait or poll on the event object.
If lpCompletionRoutine is not null, the hEvent member 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-nulllpCompletionRoutine and later calls WSAGetOverlappedResult for the same overlapped I/O request may not set the fWait parameter for that invocation of WSAGetOverlappedResult to TRUE. In this case the usage of the hEvent member is undefined, and attempting to wait on the hEvent member 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. Since 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 Ws2_32.dll offers an asynchronous procedure call (APC) mechanism to facilitate invocation of completion routines.
A service provider arranges for a function to be executed in the proper thread by calling WPUQueueApc. This function can be called from any process and thread context, even a context different from the thread and process that was used to initiate the overlapped operation.
The WPUQueueApc function takes as input parameters a pointer to a WSATHREADID structure (supplied to the provider through the lpThreadId input parameter), a pointer to an APC function to be invoked, and a context value that is subsequently passed to the APC function. Because only a single context value is available, the APC function itself cannot be the client specified–completion routine. The service provider must instead supply a pointer to its own APC function, which uses the supplied context value to access the needed result information for the overlapped operation, and then invokes the client specified–completion routine.
The prototype for the client-supplied completion routine is as follows:
void CALLBACK CompletionRoutine ( IN DWORD dwError, IN DWORD cbTransferred, IN LPWSAOVERLAPPED lpOverlapped, IN DWORD dwFlags );
The CompletionRoutine is a placeholder for a client-supplied function name. dwError specifies the completion status for the overlapped operation as indicated by lpOverlapped. cbTransferred specifies the number of bytes sent. No flag values are currently defined and the dwFlags value will be zero. This function does not return a value.
The completion routines can be called in any order, though not necessarily in the same order that the overlapped operations are completed. However, the service provider guarantees to the client that posted buffers are sent in the same order they are supplied.
Note All I/O initiated by a given thread is canceled when that thread exits. For overlapped sockets, pending asynchronous operations can fail if the thread is closed before the operations complete. See ExitThread for more information.
Minimum supported client
Windows 2000 Professional [desktop apps only]
Minimum supported server
Windows 2000 Server [desktop apps only]