Perl
Chilkat2 Perl
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 Perl Downloads
use strict;
use warnings;
use Chilkat2;
my $socket = Chilkat2::Socket->new;
# Connect to the server using TLS.
my $bTls = 1;
my $maxWaitMs = 5000;
my $success = $socket->Connect('example.com', 5000, $bTls, $maxWaitMs);
if (!$success) {
print $socket->LastErrorText, "\n";
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.
my $messageLength = 512;
$success = $socket->SendCount($messageLength);
if (!$success) {
print $socket->LastErrorText, "\n";
exit;
}
print "Length prefix sent.\n";