Sample code for 30+ languages & platforms
PureBasic

Receive Bytes as Encoded Text from a Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveBytesENC, which receives one delivery of binary data and returns it encoded as text using a named encoding.

Background. The ENC methods move raw binary data to and from printable text using a named encoding such as base64 or hex, which is convenient for logging or for handling binary data as strings.

Chilkat PureBasic Downloads

PureBasic
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 return it encoded as text using the named encoding.  This
    ;  is convenient for logging or transporting binary data as printable text.
    encoded.s = CkSocket::ckReceiveBytesENC(socket,"base64")
    If CkSocket::ckLastMethodSuccess(socket) = 0
        Debug CkSocket::ckLastErrorText(socket)
        CkSocket::ckDispose(socket)
        ProcedureReturn
    EndIf

    Debug encoded


    CkSocket::ckDispose(socket)


    ProcedureReturn
EndProcedure