Sample code for 30+ languages & platforms
PowerBuilder

TCP Socket Send Byte and Receive Byte

Demonstrates the Chilkat Socket ReceiveByte and SendByte method.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Sock
integer li_UseTls
integer li_Port
integer li_MaxWaitMs
integer li_TreatAsUnsignedInt

li_Success = 0

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

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

// --------------------------------------------------------------------
// This example uses the public TCP echo service at https://tcpbin.com/
// --------------------------------------------------------------------

li_UseTls = 0
li_Port = 4242
li_MaxWaitMs = 5000
li_Success = loo_Sock.Connect("tcpbin.com",li_Port,li_UseTls,li_MaxWaitMs)
if li_Success = 0 then
    Write-Debug loo_Sock.LastErrorText
    destroy loo_Sock
    return
end if

// Wait a max of 2 seconds for a response..
loo_Sock.MaxReadIdleMs = 2000

// Send a byte.
loo_Sock.SendByte(96)

// The tcpbin.com echo server only echoes after receiving an LF (linefeed char)
loo_Sock.SendByte(10)

// The echo server will echo back whatever is sent to it.
// We should be able to read the same byte back..
// After successfully reading, the byte value is available in the ReceivedInt property.
li_TreatAsUnsignedInt = 1
li_Success = loo_Sock.ReceiveByte(li_TreatAsUnsignedInt)
if li_Success = 0 then
    Write-Debug loo_Sock.LastErrorText
    destroy loo_Sock
    return
end if

// Let's look at the value of the byte received.  It should be 96.
Write-Debug string(loo_Sock.ReceivedInt)

// The echo server also echoed the LF back.
li_Success = loo_Sock.ReceiveByte(li_TreatAsUnsignedInt)
// Assuming success..

// Should be decimal 10.
Write-Debug string(loo_Sock.ReceivedInt)


destroy loo_Sock