Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSocket
LOCAL nBTls
LOCAL nMaxWaitMs
LOCAL nMessageLength

nSuccess := 0

oSocket := CreateObject("Chilkat.Socket")

//  Connect to the server using TLS.
nBTls := 1
nMaxWaitMs := 5000
nSuccess := oSocket:Connect("example.com", 5000, nBTls, nMaxWaitMs)
IF (nSuccess == 0)
    ? oSocket:LastErrorText
    oSocket:destroy()
    RETURN
ENDIF

//  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.
nMessageLength := 512
nSuccess := oSocket:SendCount(nMessageLength)
IF (nSuccess == 0)
    ? oSocket:LastErrorText
    oSocket:destroy()
    RETURN
ENDIF

? "Length prefix sent."

oSocket:destroy()