Sample code for 30+ languages & platforms
DataFlex

Wait for a Socket to Become Writable

See more Socket/SSL/TLS Examples

Demonstrates Socket.SelectForWriting, which waits for the socket, or sockets in a socket set, to become writable. It returns 1 if writable, 0 on timeout, or -1 on error.

Background. A timeoutMs of 0 performs a nonblocking poll. This is useful before attempting to send when the peer may be applying flow control.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSocket
    Boolean iBTls
    Integer iMaxWaitMs
    Integer iReady
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatSocket)) To hoSocket
    If (Not(IsComObjectCreated(hoSocket))) Begin
        Send CreateComObject of hoSocket
    End

    //  Connect to the server using TLS.
    Move True To iBTls
    Move 5000 To iMaxWaitMs
    Get ComConnect Of hoSocket "example.com" 5000 iBTls iMaxWaitMs To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Wait up to 5 seconds for the socket (or sockets in a socket set) to become writable.  Returns 1 if
    //  writable, 0 on timeout, or -1 on error.
    Get ComSelectForWriting Of hoSocket 5000 To iReady
    If (iReady < 0) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    If (iReady <> 1) Begin
        Showln "The socket did not become writable before the timeout."
        Procedure_Return
    End

    Showln "The socket is ready for writing."


End_Procedure