Sample code for 30+ languages & platforms
PureBasic

Receive Bytes into a BinData

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveBd, which receives one delivery of binary data and appends it to a BinData. It returns whatever is immediately available.

Background. The BinData receive methods accumulate raw bytes in a BinData object. A single receive returns only what is currently available, whereas the counted receive waits for an exact number of bytes.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkSocket.pb"

Procedure ChilkatExample()

    success.i = 0

    socket.i = CkSocket::ckCreate()
    If socket.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Connect to the server using TLS.
    bTls.i = 1
    maxWaitMs.i = 5000
    success = CkSocket::ckConnect(socket,"example.com",5000,bTls,maxWaitMs)
    If success = 0
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        ProcedureReturn
    EndIf

    ;  Receive one delivery of binary data and append it to a BinData.  ReceiveBd returns whatever is
    ;  immediately available; it does not loop to drain everything that may follow.
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkSocket::ckReceiveBd(socket,bd)
    If success = 0
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    Debug "Received " + Str(CkBinData::ckNumBytes(bd)) + " bytes."


    CkSocket::ckDispose(socket)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure