Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

$oSocket = ObjCreate("Chilkat.Socket")

;  Connect to the server using TLS.
Local $bTls = True
Local $iMaxWaitMs = 5000
$bSuccess = $oSocket.Connect("example.com",5000,$bTls,$iMaxWaitMs)
If ($bSuccess = False) Then
    ConsoleWrite($oSocket.LastErrorText & @CRLF)
    Exit
EndIf

;  Read a four-byte length prefix as a signed 32-bit integer (network byte order by default).  A
;  return value of -1 indicates failure.
Local $iMessageLength = $oSocket.ReceiveCount()
If ($iMessageLength < 0) Then
    ConsoleWrite($oSocket.LastErrorText & @CRLF)
    Exit
EndIf

ConsoleWrite("The peer will send " & $iMessageLength & " bytes." & @CRLF)