Sample code for 30+ languages & platforms
DataFlex

Receive an Exact Number of Bytes into a BinData

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveBdN, which receives exactly the requested number of bytes and appends them to a BinData, continuing until the full count is satisfied.

Background. Inactivity is controlled by MaxReadIdleMs: the call fails if incoming bytes halt for longer than MaxReadIdleMs milliseconds before the full amount is received. A single receive returns only what is currently available, whereas this counted receive waits for an exact number of bytes.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSocket
    Boolean iBTls
    Integer iMaxWaitMs
    Variant vBd
    Handle hoBd
    String sTemp1
    Integer iTemp1

    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

    //  MaxReadIdleMs controls the inactivity limit for receiving.  The ReceiveBdN call will fail if
    //  incoming bytes halt for longer than this many milliseconds before the full amount is received.
    Set ComMaxReadIdleMs Of hoSocket To 15000

    //  Receive exactly the requested number of bytes, appending them to a BinData.  The method continues
    //  receiving until the full count is satisfied or a failure occurs.
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get pvComObject of hoBd to vBd
    Get ComReceiveBdN Of hoSocket 256 vBd To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumBytes Of hoBd To iTemp1
    Showln "Received " iTemp1 " bytes."


End_Procedure