Topic: How detect disconnect or error socket?

I am writing an SSL client based on non-blocking sockets. I could connect, but I can’t understand how to detect socket errors? I have the code:

void CSecureTCPClient::DoRecv( )
{
    if ( m_Socket == INVALID_SOCKET || m_HasError || !m_Connected )
        return;

    char buffer[1024];
    int c = wolfSSL_read( m_SSL, buffer, sizeof( buffer ) - 1 );

    CONSOLE_Print( UTIL_ToString( GetLastError( ) ) );

    if ( c > 0 )
    {
        m_RecvBuffer += string( buffer, c );
        m_LastRecv = GetTime( );
    }
    else if ( c == 0 )
        return;
    else
    {
        int ss = wolfSSL_want_read( m_SSL );
        int err = wolfSSL_get_error( m_SSL, 0 );

        if ( err == EWOULDBLOCK )
            return;

        m_HasError = true;
        m_Error = err;

        char errorString[80];
        wolfSSL_ERR_error_string( err, errorString );
        CONSOLE_Print( "[SECURETCPCLIENT] error read ( " + string( errorString ) + " )" );
    }



}

void CSecureTCPClient::DoSend( )
{
    if ( m_Socket == INVALID_SOCKET || m_HasError || !m_Connected )
        return;

    if ( wolfSSL_write( m_SSL, m_SendBuffer.c_str( ), m_SendBuffer.size( ) ) == m_SendBuffer.size( ) ) {

        int c = wolfSSL_want_write( m_SSL );

        if ( c >= 0 )
        {
            m_SendBuffer = m_SendBuffer.substr( c );
            return;
        }

        int err = wolfSSL_get_error( m_SSL, 0 );

        if ( err == EWOULDBLOCK )
            return;

        char errorString[80];
        m_Error = err;
        wolfSSL_ERR_error_string( err, errorString );

        CONSOLE_Print( "[SECURETCPCLIENT] error send ( " + string( errorString ) + " )" );
        m_HasError = true;
        return;
    }
}

How to understand where the remote party closed the connection? And where did the socket error go? Does the EWOULDBLOCK library itself handle, or should its case also be taken into account?

Share

Re: How detect disconnect or error socket?

Hi @kirill-782,

Have you seen our non-blocking client example on github?
https://github.com/wolfSSL/wolfssl-exam … blocking.c

If not that would be a good place to start. Can you tell us what it is you are working and and the end-goals for your project?

Warm Regards,

K