Winsock Completion Indications
A version of this page is also available for
4/8/2010
Winsock provides several options for receiving completion indications, thus providing applications with appropriate levels of flexibility. These include blocking and waiting, on event objects, polling event objects, and socket I/O completion routines.
Blocking and Waiting for Completion Indication
Applications can block while waiting for one or more event objects to become set using the WaitForMultipleObjects function.
Polling for Completion Indication
Applications that prefer not to block can use the WSAGetOverlappedResult function to poll for the completion status associated with any particular event object. This function indicates whether or not the overlapped operation has completed, and if completed, arranges for the WSAGetLastError function to retrieve the error status of the overlapped operation.
Using Socket I/O Completion Routines
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 create a thread for each call that specifies a completion routine. Because each function call requires a separate thread, using completion routines with overlapped I/O can quickly become very memory-consuming. Using events is recommended instead. Windows Embedded CE does not signal events for overlapped I/O operations that have been completed successfully.
The functions used to initiate overlapped I/O (WSASend, WSASendTo, WSARecv, WSARecvFrom) all take lpCompletionRoutine as an optional input parameter. This is a pointer to an application-specific function that is called after a successfully initiated overlapped I/O operation completes, successfully or otherwise. The completion routine follows the same rules as stipulated for Win32 file I/O completion routines. That is, the completion routine is not invoked until the thread is in an alertable wait state. An application that uses the completion routine option for a particular overlapped I/O request may not use the wait option of WSAGetOverlappedResult for that same overlapped I/O request.
The transports 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.