The information in this article applies to:
SUMMARY
CSocket operations, such as Receive, Send, and Connect, are blocking
operations in the sense that a call to these functions will not return
until it has successfully completed or an error on the socket has occurred.
MORE INFORMATIONOne approach would be to set a timer that fires when the operation has taken too long. The key to this approach is in how the timer is handled. Although the operation is "blocking," you still have the ability to handle messages that arrive. If you set a timer by using SetTimer, then you can look for the WM_TIMER message, and abort the operation when it arrives. The primary functions involved in this process are: The Windows API call:::SetTimer The MFC functions:For simplicity, this functionality can be encapsulated in your CSocket- derived class.CSocket::OnMessagePending WARNING: Before reading further, note that there is a bug in some versions of MFC that will cause problems if you attempt to use a timer and override OnMessagePending. This problem is documented in the following Microsoft Knowledge Base article: Q137632 BUG: OnMessagePending Not Called When a Timer Is ActiveThis article applies only to versions 1.52, 1.52b, 2.1, and 2.2 of Visual C++. If you are using one of these versions of Visual C++, then you will also need to implement the workaround that is supplied. The sample code for a class that provides this time-out capability is included at the end of this article. The functions implemented by the class are described in the following sections of this article. BOOL SetTimeOut(UINT uTimeOut)This would be called immediately before calling the CSocket function (for example, Receive, Send, and Accept). The uTimeOut parameter is specified in milliseconds. The following implementation simply sets up the timer. This function returns FALSE if the attempt to set the timer failed. See the Windows API documentation on the SetTimer function for further details.BOOL KillTimeOut()This function needs to be called after the completion of the operation that was blocking. It removes the timer that was set up with SetTimeOut. It returns FALSE if the call to KillTimer failed. See the Windows API documentation on the KillTimer function for further details.BOOL OnMessagePending()This is a virtual callback that is called by the CSocket class when it is waiting for an operation to complete. It gives you the opportunity to do something with incoming messages. This implementation checks for the WM_TIMER message for the timer set with the SetTimeOut call. And if it arrives then it invokes the CancelBlockingCall function. See the MFC documentation on the OnMessagePending and CancelBlockingCall functions for details. Please note that by calling CancelBlockingCall, you will cause the operation to fail and GetLastError will return WSAEINTR (indicating an interrupted operation).Here is an example use of this class:
Sample Code
REFERENCES
For more information on the SetTimer and KillTimer functions, please see
the Windows API Help file.
In Visual C++ 2.x - The MFC Help file. Additional query words: timing
Keywords : kbnetwork kbAPI kbMFC kbSDKPlatform kbWinsock kbCodeSam kbGrpNet |
Last Reviewed: July 23, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |