Sample code for 30+ languages & platforms
PureBasic

Receive Bytes up to a Delimiter into a BinData

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveUntilByteBd, which reads bytes until a delimiter byte is encountered and appends all bytes up to and including the delimiter to a BinData.

Background. The delimiter is a raw byte value from 0 through 255. This is useful for framing schemes that terminate each message with a known delimiter byte.

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

    ;  Read bytes until the delimiter byte is encountered, appending all bytes up to and including the
    ;  delimiter to a BinData.  The delimiter is a raw byte value from 0 through 255 (here the linefeed
    ;  byte, decimal 10).
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

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


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


    ProcedureReturn
EndProcedure