The WSASendMsg function sends data and optional control information from connected and unconnected sockets.
int WSAAPI WSASendMsg( SOCKET Handle, LPWSAMSG lpMsg, DWORD dwFlags, LPDWORD lpNumberOfBytesSent, LPWSAOVERLAPPED lpOverlapped, LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine );
A descriptor identifying the socket.
A WSAMSG structure storing the Posix.1g msghdr structure.
The flags used to modify the behavior of the WSASendMsg function call. For more information, see Using dwFlags in the Remarks section.
A pointer to the number, in bytes, sent by this call if the I/O operation completes immediately.
Use NULL for this parameter if the lpOverlapped parameter is not NULL to avoid potentially erroneous results. This parameter can be NULL only if the lpOverlapped parameter is not NULL.
A pointer to a WSAOVERLAPPED structure. Ignored for non-overlapped sockets.
A pointer to the completion routine called when the send operation completes. Ignored for non-overlapped sockets.
Returns zero when successful and immediate completion occurs. When zero is returned, the specified completion routine is called when the calling thread is in the alertable state.
A return value of SOCKET_ERROR, and subsequent call to WSAGetLastError that returns WSA_IO_PENDING, indicates the overlapped operation has successfully initiated; completion is then indicated through other means, such as through events or completion ports.
Upon failure, returns SOCKET_ERROR and a subsequent call to WSAGetLastError returns a value other than WSA_IO_PENDING. The following table lists error codes.
|The requested address is a broadcast address, but the appropriate flag was not set.|
|For a UDP datagram socket, this error would indicate that a previous send operation resulted in an ICMP "Port Unreachable" message.|
|The lpMsg, lpNumberOfBytesSent, lpOverlapped, or lpCompletionRoutine parameter is not totally contained in a valid part of the user address space. This error is also returned if a name member of the WSAMSGstructure pointed to by the lpMsg parameter was a NULL pointer and the namelen member of the WSAMSGstructure was not set to zero. This error is also returned if a Control.buf member of the WSAMSGstructure pointed to by the lpMsg parameter was a NULL pointer and the Control.len member of the WSAMSGstructure was not set to zero.|
|A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.|
|A blocking Windows Socket 1.1 call was canceled through WSACancelBlockingCall.|
|The socket has not been bound with bind, or the socket was not created with the overlapped flag.|
|The socket is message oriented, and the message is larger than the maximum supported by the underlying transport.|
|The network subsystem has failed.|
|For a datagram socket, this error indicates that the time to live has expired.|
|The network is unreachable.|
|The Windows Sockets provider reports a buffer deadlock.|
|The socket is not connected.|
|The descriptor is not a socket.|
|The socket operation is not supported. This error is returned if the dwFlags member of the WSAMSGstructure pointed to by the lpMsg parameter includes any control flags invalid for WSASendMsg.|
|The socket has been shut down; it is not possible to call the WSASendMsg function on a socket after shutdown has been invoked with how set to SD_SEND or SD_BOTH.|
|The socket timed out. This error is returned if the socket had a wait timeout specified using the SO_SNDTIMEO socket option and the timeout was exceeded.|
|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.|
|A successful WSAStartup call must occur before using this function.|
|An overlapped operation was successfully initiated and completion will be indicated at a later time.|
|The overlapped operation has been canceled due to the closure of the socket or due to the execution of the SIO_FLUSH command in WSAIoctl.|
The WSASendMsg function can be used in place of the WSASend and WSASendTo functions. The WSASendMsg function can only be used with datagrams and raw sockets. The socket descriptor in the s parameter must be opened with the socket type set to SOCK_DGRAM or SOCK_RAW.
The dwFlags parameter can only contain a combination of the following control flags: MSG_DONTROUTE, MSG_PARTIAL, and MSG_OOB. The dwFlags member of the WSAMSGstructure pointed to by the lpMsg parameter is ignored on input and not used on output.
Overlapped sockets are created with a WSASocket function call that has the WSA_FLAG_OVERLAPPED flag set. For overlapped sockets, sending information uses overlapped I/O unless both lpOverlapped and lpCompletionRoutine are NULL; when lpOverlapped and lpCompletionRoutine are NULL, the socket is treated as a nonoverlapped socket. A completion indication occurs with overlapped sockets; once the buffer or buffers have been consumed by the transport, a completion routine is triggered or an event object is set. If the operation does not complete immediately, the final completion status is retrieved through the completion routine or by calling the WSAGetOverlappedResult function.
For nonoverlapped sockets, the lpOverlapped and lpCompletionRoutine parameters are ignored and WSASendMsg adopts the same blocking semantics as the send function: data is copied from the buffer or buffers into the transport's buffer. If the socket is nonblocking and stream oriented, and there is insufficient space in the transport's buffer, WSASendMsg returns with only part of the application's buffers having been consumed. In contrast, this buffer situation on a blocking socket results in WSASendMsg blocking until all of the application's buffer contents have been consumed.
If this function is completed in an overlapped manner, it is the Winsock service provider's responsibility to capture this WSABUF structure before returning from this call. This enables applications to build stack-based WSABUF arrays pointed to by the lpBuffers member of the WSAMSGstructure pointed to by the lpMsg parameter.
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.
On an IPv4 socket of type SOCK_DGRAM or SOCK_RAW, an application can specific the local IP source address to use for sending with the WSASendMsg function. One of the control data objects passed in the WSAMSG structure to the WSASendMsg function may contain an in_pktinfo structure used to specify the local IPv4 source address to use for sending.
On an IPv6 socket of type SOCK_DGRAM or SOCK_RAW, an application can specific the local IP source address to use for sending with the WSASendMsg function. One of the control data objects passed in the WSAMSG structure to the WSASendMsg function may contain an in6_pktinfo structure used to specify the local IPv6 source address to use for sending.
For a dual-stack socket when sending datagrams with the WSASendMsg function and an application wants to specify a specific local IP source address to be used, the method to handle this depends on the destination IP address. When sending to an IPv4 destination address or an IPv4-mapped IPv6 destination address, one of the control data objects passed in the WSAMSG structure pointed to by the lpMsg parameter should contain an in_pktinfo structure containing the local IPv4 source address to use for sending. When sending to an IPv6 destination address that is not a an IPv4-mapped IPv6 address, one of the control data objects passed in the WSAMSG structure pointed to by the lpMsg parameter should contain an in6_pktinfo structure containing the local IPv6 source address to use for sending.
|MSG_DONTROUTE||Specifies that the data should not be subject to routing. A Windows Sockets service provider can choose to ignore this flag.|
|MSG_PARTIAL||Specifies that lpMsg->lpBuffers contains only a partial message. Note that the error code WSAEOPNOTSUPP will be returned by transports that do not support partial message transmissions.|
The possible values for dwFlags parameter are defined in the Winsock2.h header file.
On output, the dwFlags member of the WSAMSGstructure pointed to by the lpMsg parameter is not used.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 WSASendMsg function using overlapped I/O can be called from within the completion routine of a previous , WSARecv, WSARecvFrom, WSARecvMsg, WSASend, WSASendMsg, or WSASendTo function. 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 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 WSAWaitForMultipleEvents or 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 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 completion routine follows the same rules as stipulated for Windows file I/O completion routines. The completion routine will not be invoked until the thread is in an alertable wait state, for example, with WSAWaitForMultipleEvents called with the fAlertable parameter set to TRUE.
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 preemptive context.
The prototype of the completion routine is as follows.
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 the lpOverlapped parameter. The cbTransferred parameter indicates the number of bytes sent. Currently there are no flag values defined and the dwFlags parameter will be zero. The CompletionRoutine function does not return a value.
Returning from this function allows invocation of another pending completion routine for the 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 the overlapped operations are completed. However, the posted buffers are guaranteed to be sent in the same order they are specified.
Windows 8.1 and Windows Server 2012 R2: This function is supported for Windows Store apps on Windows 8.1, Windows Server 2012 R2, and later.
|Minimum supported client||Windows 8.1, Windows Vista [desktop apps | UWP apps]|
|Minimum supported server||Windows Server 2008 [desktop apps | UWP apps]|
|Header||winsock2.h (include Mswsock.h)|