Sample code for 30+ languages & platforms
PureBasic

Bidirectional Sockets (TLS or non-TLS, simultaneous reading and writing a connection)

See more Socket/SSL/TLS Examples

This example demonstrates how to simultaneously read/write on a single socket connection.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSocket.pb"
IncludeFile "CkTask.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

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

    ; We'll just use an HTTPS server for this example...
    bUseTls.i = 1
    maxWaitMs.i = 5000
    success = CkSocket::ckConnect(tlsRead,"www.chilkatsoft.com",443,bUseTls,maxWaitMs)
    If success = 0
        Debug CkSocket::ckLastErrorText(tlsRead)
        CkSocket::ckDispose(tlsRead)
        ProcedureReturn
    EndIf

    ; Chilkat classes are thread-safe.  This means that only one method call can be active
    ; at a time for a given object instance.  It would seem that this would prevent the possibility
    ; to simultaneously read/write a given connection because it would require two method calls
    ; to be simultaneously active: one for reading and one for writing.
    ; 
    ; There's a trick to doing it...
    ; 
    ; The DupSocket method is provided to get a new object instance that shares the same socket
    ; connection.  This allows for the coarse-grained object-level thread safety to be maintained, 
    ; while finer-grained thread-safety mechanisms keep things kosher internally.

    ; One object will be used for reading, and the cloned socket is used for writing.
    ; It doesn't matter which --  you can use the cloned socket for reading or the original for writing.
    ; However.. if you try to read simultneously from both the original and cloned objects at the same
    ; time, then one will block until the other finishes.  (This is because of the finer-grained thread
    ; safety internally.)  The same is true if you try to write both socket objects simultaneously.

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

    success = CkSocket::ckDupSocket(tlsRead,tlsWrite)
    If success = 0
        Debug CkSocket::ckLastErrorText(tlsRead)
        CkSocket::ckDispose(tlsRead)
        CkSocket::ckDispose(tlsWrite)
        ProcedureReturn
    EndIf

    ; Let's start an async read on the socket.  Nothing will be arriving until we actually send the GET
    ; request and the server responds.  This will read until the end of the HTTP response header.
    task.i = CkSocket::ckReceiveUntilMatchAsync(tlsRead,Chr(13) + Chr(10) + Chr(13) + Chr(10))
    CkTask::ckRun(task)

    ; Now send the request.  This should not block because the read is happening on the tlsRead object.
    httpGetReq.s = "GET / HTTP/1.1" + Chr(13) + Chr(10) + "Host: www.chilkatsoft.com" + Chr(13) + Chr(10) + Chr(13) + Chr(10)
    success = CkSocket::ckSendString(tlsWrite,httpGetReq)
    ; Assuming success for the example...

    ; Wait for the read task to finish.
    ; The 1/0 returned by Wait applies to the Wait method call, not the task.
    maxWaitMs = 5000
    success = CkTask::ckWait(task,maxWaitMs)
    If Not success OR (CkTask::ckStatusInt(task) <> 7) OR (CkTask::ckTaskSuccess(task) <> 1)
        If Not success
            ; The task.LastErrorText applies to the Wait method call.
            Debug CkTask::ckLastErrorText(task)
        Else
            ; The ResultErrorText applies to the underlying task method call (i.e. the Connect)
            Debug CkTask::ckStatus(task)
            Debug CkTask::ckResultErrorText(task)
        EndIf

        CkTask::ckDispose(task)

        CkSocket::ckDispose(tlsRead)
        CkSocket::ckDispose(tlsWrite)
        ProcedureReturn
    EndIf

    ; Examine the received HTTP response header:
    Debug "HTTP response header:"
    Debug CkTask::ckGetResultString(task)

    ; We should get a response that looks like this:
    ; 	HTTP response header:
    ; 	HTTP/1.1 200 OK
    ; 	Cache-Control: private
    ; 	Content-Length: 7477
    ; 	Content-Type: text/html
    ; 	Server: Microsoft-IIS/8.5
    ; 	Set-Cookie: ASPSESSIONIDSWDSTRTQ=BBNMIKGCHFJNILFFPLDIOGDE; secure; path=/
    ; 	X-Powered-By: ASP.NET
    ; 	X-Powered-By-Plesk: PleskWin
    ; 	Date: Thu, 06 Apr 2017 12:03:30 GMT

    CkTask::ckDispose(task)

    ; Forget about the remainder of the HTTP response... The example was only to demonstrate
    ; simultaneous reading/writing..
    maxWaitMs = 20
    CkSocket::ckClose(tlsRead,maxWaitMs)


    CkSocket::ckDispose(tlsRead)
    CkSocket::ckDispose(tlsWrite)


    ProcedureReturn
EndProcedure