Sample code for 30+ languages & platforms
PowerBuilder

Receive a 4-byte Length Prefix from a Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveCount, which reads a four-byte length prefix as a signed 32-bit integer. A return value of -1 indicates failure.

Background. Reading the length prefix first tells the application exactly how many bytes to read for the message body, which is a reliable way to know when a full message has arrived.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Socket
integer li_BTls
integer li_MaxWaitMs
integer li_MessageLength

li_Success = 0

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

//  Connect to the server using TLS.
li_BTls = 1
li_MaxWaitMs = 5000
li_Success = loo_Socket.Connect("example.com",5000,li_BTls,li_MaxWaitMs)
if li_Success = 0 then
    Write-Debug loo_Socket.LastErrorText
    destroy loo_Socket
    return
end if

//  Read a four-byte length prefix as a signed 32-bit integer (network byte order by default).  A
//  return value of -1 indicates failure.
li_MessageLength = loo_Socket.ReceiveCount()
if li_MessageLength < 0 then
    Write-Debug loo_Socket.LastErrorText
    destroy loo_Socket
    return
end if

Write-Debug "The peer will send " + string(li_MessageLength) + " bytes."


destroy loo_Socket