Hi,
   I think  the problem is that you never waited on a close
event and you did not check error code SSL_ERROR_ZERO_RETURN
which signals connection termination. Besides the structure 
of your code too didnt look very clean. I have code below 
which I feel should work fine. 
You need not really wait on FD_WRITE, as I believe it would
cause the Wait to return immediately and therefore CPU usage
would shoot up. On WANT_WRITE, it should be ok to wait on read
event. (Unless you have something else in mind besides handling
WANT_WRITE).
You could very well WaitForSingleObject since there's just 
one handle you are waiting on.

.....
..... 
WSAEventSelect( iSocketID, m_hSocketEvent, FD_READ|FD_CLOSE);

while(1)
{
    int iResult = WSAWaitForMultipleEvents( 1, &m_hSocketEvent, FALSE,
30000, FALSE );
    if( iResult == WSA_WAIT_EVENT_0 )
    {
        WSANETWORKEVENTS networkEvents;
        iRetCode = WSAEnumNetworkEvents(iSocketID, m_hSocketEvent,
&networkEvents);
        long lEvents = networkEvents.lNetworkEvents;
        if(lEvents & FD_CLOSE) //close
        {
            //cleanup and break;
        }
        //Assuming an active read is all you wanna do
        if(lEvents & FD_READ || lEvents & FD_WRITE) //
        {
            int readBytes = Read();
            if(readBytes > 0) //application data
            {
                //do whatever processing 
            }
            
            if(readBytes == -2) //error terminate connection
            {
                //cleanup and break;
            }
            continue; //if readBytes == -1 then go back to WaitFor...
            //I think SSL_read returns zero when it reads handshake
data
            //so we really donot have to bother about it 
        }
    } //end of if
} //end of while
....
     



//Returns -1 if error, otherwise bytes read, 
Read() 
{     
   
   int iErr = SSL_read(m_sslconn, szBuffOut, sizeof(szBuffOut));
   if( iErr >= 0) //read bytes, could be handshake as well
       return iErr;  //return bytes Read 
   else 
   {
       int iLastErr = SSL_get_error( m_sslconn, iErr );
       if( iLastErr == SSL_ERROR_WANT_READ  || iLastErr ==
SSL_ERROR_WANT_WRITE )
           return -1;
       //SSL_ERROR_ZERO_RETURN is actually closure alert, the others
are other errors
       if ( iLastError == SSL_ERROR_ZERO_RETURN || SSL_ERROR_SYSCALL
|| SSL_ERROR_SSL)
           return -2;
   } //end of else
} //end of read
          
       
I hope this is correct and helpful.

Amit.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to