Microsoft KB Archive/154632

= FIX: Accept Returns FALSE but GetLastError Returns 0 =

Article ID: 154632

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition 

-

<div class="notice_section">

This article was previously published under Q154632

<div class="notice_section">

<div class="symptoms_section">

SYMPTOMS
A call to CSocket or CAsyncSocket::Accept returns FALSE, but a call to GetLastError or WSAGetLastError returns 0. The same code works correctly with MFC version 4.0.

<div class="cause_section">

CAUSE
MFC maintains information in Thread Local Storage. In CAsyncSocket::Accept, the MFC code makes a call to DetachHandle that indirectly makes a call to TlsGetValue. This occurs after a call to the WinSock API function accept. The call to TlsGetValue resets the thread's error code to zero and the error returned from the accept call is lost.

<div class="resolution_section">

RESOLUTION
If your class is derived directly from CAsyncSocket, then replace the Accept function and modify it so that the error code is not lost.

This can be done by first borrowing the function from the CAsyncSocket::Accept function found in the MFC source code in the file SOCKCORE.CPP. Change the function to be implemented as follows: #if _MFC_VER >= 0x0410 && _MFC_VER <= 0x0420

BOOL CMySocket::Accept(CAsyncSocket& rConnectedSocket,                         SOCKADDR* lpSockAddr, int* lpSockAddrLen) {    ASSERT(rConnectedSocket.m_hSocket == INVALID_SOCKET); ASSERT(CAsyncSocket::FromHandle(INVALID_SOCKET) == NULL);

CAsyncSocket::AttachHandle(INVALID_SOCKET, &rConnectedSocket);

SOCKET hTemp = accept(m_hSocket, lpSockAddr, lpSockAddrLen);

if (hTemp == INVALID_SOCKET) {  // <===== STORE THE ERROR CODE RECEIVED FROM THE accept CALL int nError = WSAGetLastError;

CAsyncSocket::DetachHandle(rConnectedSocket.m_hSocket, FALSE);

// <===== RE-SET THE ERROR CODE THAT WAS SET IN THE accept CALL WSASetLastError(nError);

rConnectedSocket.m_hSocket = INVALID_SOCKET; }    else if (CAsyncSocket::FromHandle(INVALID_SOCKET) != NULL) {      rConnectedSocket.m_hSocket = hTemp; CAsyncSocket::DetachHandle(INVALID_SOCKET, FALSE); CAsyncSocket::AttachHandle(hTemp, &rConnectedSocket); }

return (hTemp != INVALID_SOCKET); }

#endif // _MFC_VER If your class is derived from CSocket, then you will have to replace CAsyncSocket::Accept as well as CSocket::Accept. This is necessary because CSocket::Accept directly calls CAsyncSocket::Accept. This can be accomplished by providing your own Accept function, called AcceptFix. It would be implemented just as the Accept function shown above. For example: #if _MFC_VER >= 0x0410 && _MFC_VER <= 0x0420

BOOL CMySocket::AcceptFix(CAsyncSocket& rConnectedSocket,                            SOCKADDR* lpSockAddr, int* lpSockAddrLen) {     ...      // IMPLEMENTATION IS THE SAME AS THE ONE SHOWN ABOVE ...     return (hTemp != INVALID_SOCKET); }

#endif // _MFC_VER Then provide a replacement for CSocket::Accept and modify it to call AcceptFix instead of CAsyncSocket::Accept. You can borrow the CSocket::Accept function from SOCKCORE.CPP and modify it as follows: #if _MFC_VER >= 0x0410 && _MFC_VER <= 0x0420

BOOL CMySocket::Accept(CAsyncSocket& rConnectedSocket,                         SOCKADDR* lpSockAddr, int* lpSockAddrLen) {    if (m_pbBlocking != NULL) {      WSASetLastError(WSAEINPROGRESS); return FALSE; }

// <=============== REPLACE THE FOLLOWING LINE // while (!CAsyncSocket::Accept(rConnectedSocket, lpSockAddr,... // <=============== WITH THIS:

while (!AcceptFix(rConnectedSocket, lpSockAddr, lpSockAddrLen)) {      if (GetLastError == WSAEWOULDBLOCK) {        if (!PumpMessages(FD_ACCEPT)) return FALSE; }      else return FALSE; }    return TRUE; }

#endif // _MFC_VER

<div class="status_section">

STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been corrected in the Visual C++ 4.2b technology update.

Keywords: kbbug kbfix kbvc420fix kbwinsock KB154632

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.