Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

set socket [new_CkSocket]

#  Connect to the server using TLS.
set bTls 1
set maxWaitMs 5000
set success [CkSocket_Connect $socket "example.com" 5000 $bTls $maxWaitMs]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSocket $socket
    exit
}

#  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.
set messageLength 512
set success [CkSocket_SendCount $socket $messageLength]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSocket $socket
    exit
}

puts "Length prefix sent."

delete_CkSocket $socket