Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_TlsRead
integer li_BUseTls
integer li_MaxWaitMs
oleobject loo_TlsWrite
oleobject loo_Task
string ls_HttpGetReq

li_Success = 0

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

loo_TlsRead = create oleobject
li_rc = loo_TlsRead.ConnectToNewObject("Chilkat.Socket")
if li_rc < 0 then
    destroy loo_TlsRead
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// We'll just use an HTTPS server for this example...
li_BUseTls = 1
li_MaxWaitMs = 5000
li_Success = loo_TlsRead.Connect("www.chilkatsoft.com",443,li_BUseTls,li_MaxWaitMs)
if li_Success = 0 then
    Write-Debug loo_TlsRead.LastErrorText
    destroy loo_TlsRead
    return
end if

// 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.

loo_TlsWrite = create oleobject
li_rc = loo_TlsWrite.ConnectToNewObject("Chilkat.Socket")

li_Success = loo_TlsRead.DupSocket(loo_TlsWrite)
if li_Success = 0 then
    Write-Debug loo_TlsRead.LastErrorText
    destroy loo_TlsRead
    destroy loo_TlsWrite
    return
end if

// 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.
loo_Task = loo_TlsRead.ReceiveUntilMatchAsync("~r~n~r~n")
loo_Task.Run()

// Now send the request.  This should not block because the read is happening on the tlsRead object.
ls_HttpGetReq = "GET / HTTP/1.1~r~nHost: www.chilkatsoft.com~r~n~r~n"
li_Success = loo_TlsWrite.SendString(ls_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.
li_MaxWaitMs = 5000
li_Success = loo_Task.Wait(li_MaxWaitMs)
if not li_Success OR (loo_Task.StatusInt <> 7) OR (loo_Task.TaskSuccess <> 1) then
    if not li_Success then
        // The task.LastErrorText applies to the Wait method call.
        Write-Debug loo_Task.LastErrorText
    else
        // The ResultErrorText applies to the underlying task method call (i.e. the Connect)
        Write-Debug loo_Task.Status
        Write-Debug loo_Task.ResultErrorText
    end if

    destroy loo_Task
    destroy loo_TlsRead
    destroy loo_TlsWrite
    return
end if

// Examine the received HTTP response header:
Write-Debug "HTTP response header:"
Write-Debug loo_Task.GetResultString()

// 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

destroy loo_Task

// Forget about the remainder of the HTTP response... The example was only to demonstrate
// simultaneous reading/writing..
li_MaxWaitMs = 20
loo_TlsRead.Close(li_MaxWaitMs)


destroy loo_TlsRead
destroy loo_TlsWrite