Sample code for 30+ languages & platforms
PureBasic

Send BinData Contents over a Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.SendBd, which sends bytes from a BinData beginning at an offset. A numBytes of 0 sends from the offset through the end of the data.

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

    ;  Place some bytes into a BinData to be sent.
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkBinData::ckAppendString(bd,"message payload","utf-8")

    ;  Send bytes from the BinData starting at the given offset.  A numBytes of 0 sends from the offset
    ;  through the end of the data.
    success = CkSocket::ckSendBd(socket,bd,0,0)
    If success = 0
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

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


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


    ProcedureReturn
EndProcedure