B4X
B4X
Send a 4-byte Length Prefix over a Socket
See more Socket/SSL/TLS Examples
Demonstrates Socket.SendCount, which sends a four-byte length prefix in network byte order. This is typically used to tell the peer how many bytes follow.
Background. A length-prefixed framing scheme sends the byte count of a message before the message body, so the receiver knows exactly how many bytes to read with a counted receive.
Chilkat B4X Downloads
Dim success As Boolean = False
Dim socket As ChilkatSocket
socket.Initialize("socket")
' Connect to the server using TLS.
Dim bTls As Boolean = True
Dim maxWaitMs As Int = 5000
success = socket.Connect("example.com", 5000, bTls, maxWaitMs)
If success = False Then
Log(socket.LastErrorText)
Return
End If
' Send a four-byte length prefix in network byte order. A common framing pattern is to send the
' number of bytes in a message before the message itself, so the peer knows exactly how many bytes
' to read.
Dim messageLength As Int = 512
success = socket.SendCount(messageLength)
If success = False Then
Log(socket.LastErrorText)
Return
End If
Log("Length prefix sent.")