Sample code for 30+ languages & platforms
Visual Basic 6.0

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 Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

Dim socket As New ChilkatSocket

'  Connect to the server using TLS.
Dim bTls As Long
bTls = 1
Dim maxWaitMs As Long
maxWaitMs = 5000
success = socket.Connect("example.com",5000,bTls,maxWaitMs)
If (success = 0) Then
    Debug.Print socket.LastErrorText
    Exit Sub
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.
Dim messageLength As Long
messageLength = socket.ReceiveCount()
If (messageLength < 0) Then
    Debug.Print socket.LastErrorText
    Exit Sub
End If

Debug.Print "The peer will send " & messageLength & " bytes."