WSASend (Compact 2013)
3/26/2014
This function sends data on a connected socket.
Syntax
int WSASend(
SOCKET s,
LPWSABUF lpBuffers,
DWORD dwBufferCount,
LPDWORD lpNumberOfBytesSent,
DWORD dwFlags,
LPWSAOVERLAPPED lpOverlapped,
LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
Parameters
- s
[in] Descriptor identifying a connected socket.
- lpBuffers
[in] Pointer to an array of WSABUF structures. Each WSABUF structure contains a pointer to a buffer and the length of the buffer. This array must remain valid for the duration of the send operation.
- dwBufferCount
[in] Number of WSABUF structures in the lpBuffers array.
- lpNumberOfBytesSent
[out] Pointer to the number of bytes sent by this call if the I/O operation completes immediately.
- dwFlags
[in] Flags used to modify the behavior of the WSASend function call
- lpOverlapped
[in] Pointer to a WSAOVERLAPPED structure. This parameter is ignored for nonoverlapped sockets.
- lpCompletionRoutine
[in] Pointer to the completion routine called when the send operation has been completed. This parameter is ignored for nonoverlapped sockets.
Return Value
If no error occurs and the send 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 Compact, 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 WSAGetLastError. 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 WSAStartup call must occur before using this function. |
WSAENETDOWN |
The network subsystem has failed. |
WSAEACCES |
The requested address is a broadcast address, but the appropriate flag was not set. |
WSAEINTR |
The socket was closed. |
WSAEINPROGRESS |
A blocking Winsock call is in progress, or the service provider is still processing a callback function. |
WSAEFAULT |
The lpBuffers, lpNumberOfBytesSent, lpOverlapped, or lpCompletionRoutine argument is not totally contained in a valid part of the user address space. |
WSAENETRESET |
The connection has been broken due to keep-alive activity detecting a failure while the operation was in progress. |
WSAENOBUFS |
The Windows Sockets provider reports a buffer deadlock. |
WSAENOTCONN |
The socket is not connected. |
WSAENOTSOCK |
The descriptor is not a socket. |
WSAEOPNOTSUPP |
MSG_OOB was specified, but the socket is not stream style such as type SOCK_STREAM, out of band (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. |
WSAESHUTDOWN |
The socket has been shut down. It is not possible to call WSASend on a socket after shutdown has been invoked with how set to SD_SEND 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 send operation cannot be completed immediately. |
WSAEMSGSIZE |
The socket is message-oriented, and the message is larger than the maximum supported by the underlying transport. |
WSAEINVAL |
The socket has not been bound with bind (Windows Sockets) or the socket is not created with the overlapped flag. |
WSAECONNABORTED |
The virtual circuit was terminated due to a time-out or other failure. |
WSAECONNRESET |
The virtual circuit was reset 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 send function in the following two important areas:
- It can be used in conjunction with overlapped sockets to perform overlapped send operations.
- It allows multiple send buffers to be specified making it applicable to the scatter/gather type of I/O.
This function is used to write outgoing data from one or more buffers on a connection-oriented socket specified by socket s. It can also be used, however, on connectionless sockets that have a stipulated default peer address established through the connect (Windows Sockets) or WSAConnect function.
For overlapped sockets (created using WSASocket with the WSA_FLAG_OVERLAPPED flag), sending information uses overlapped I/O unless both lpOverlapped and lpCompletionRoutine are NULL. In that case, the socket is treated as a nonoverlapped socket. A completion indication will occur, invoking the completion of a routine or setting of an event object, when the supplied buffers have been consumed by the transport. If the operation does not complete immediately, the final completion status is retrieved through the completion routine or WSAGetOverlappedResult.
If both lpOverlapped and lpCompletionRoutine are NULL, the socket in this function will be treated as a nonoverlapped socket.
For nonoverlapped sockets, the last two parameters (lpOverlapped and lpCompletionRoutine) are ignored and WSASend adopts the same blocking semantics as send. Data is copied from the supplied buffers into the transport's buffer. If the socket is nonblocking and stream-oriented, and there is not sufficient space in the transport's buffer, WSASend will return with only part of the application's buffers having been consumed. Given the same buffer situation and a blocking socket, WSASend will block until all of the application's buffer contents have been consumed.
The array of WSABUF structures pointed to by the lpBuffers parameter is transient. If this operation is completed 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 provider, 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
The successful completion of a WSASend call does not indicate that the data was successfully delivered.
Using dwFlags
The dwFlags 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 following table shows the values that are used with the bitwise OR operator to construct the dwFlags parameter.
Value |
Description |
---|---|
MSG_DONTROUTE |
The MSG_DONROUTE flag is not supported by the default Windows Embedded Compact service provider. |
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. |
Overlapped Socket I/O
Note
For Windows Embedded Compact, avoid specifying completion routines for overlapped I/O operations. Because Windows Embedded Compact 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, WSASend 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, WSASend 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 WSAGetOverlappedResult.
The WSASend function can be called from within the completion routine of a previous WSARecv, WSARecvFrom, WSASend, or WSASendTo function. This permits time-sensitive data transmissions to occur entirely within a pre-emptive 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 WSAOVERLAPPED structure.
If the lpCompletionRoutine parameter is NULL, the hEvent parameter of lpOverlapped is signaled when the overlapped operation completes if it contains a valid event object handle. An application can use WSAGetOverlappedResult to wait or poll on the event object.
If lpCompletionRoutine is not NULL, the hEvent parameter is ignored and can be used by the application to pass context information to the completion routine. A caller that passes a non-NULL lpCompletionRoutine parameter 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 parameter is undefined and attempting to wait on the hEvent parameter 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 syntax shows the prototype of the completion routine.
void CALLBACK CompletionRoutine(
IN DWORD dwError,
IN DWORD cbTransferred,
IN LPWSAOVERLAPPED lpOverlapped,
IN DWORD dwFlags
);
The CompletionRoutine function is a placeholder for an application-defined or library-defined function name. The dwError parameter specifies the completion status for the overlapped operation as indicated by lpOverlapped. The cbTransferred parameter specifies the number of bytes sent. Currently there are no flag values defined and dwFlags will be zero. This function does not return a value.
Returning from this function allows invocation of another pending completion routine for this socket. All waiting completion routines are called before the alertable thread's wait is satisfied with a return code of WSA_IO_COMPLETION. The completion routines can be called in any order, not necessarily in the same order in which overlapped operations are completed. However, the posted buffers are guaranteed to be sent in the same order they are supplied.
Note
Windows Embedded Compact does not allow overlapped and nonoverlapped operations to be used simultaneously on a socket.
Requirements
Header |
winsock2.h |
Library |
Ws2.lib |