Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
socket: HCkSocket;
bTls: Boolean;
maxWaitMs: Integer;
messageLength: Integer;

begin
success := False;

socket := CkSocket_Create();

//  Connect to the server using TLS.
bTls := True;
maxWaitMs := 5000;
success := CkSocket_Connect(socket,'example.com',5000,bTls,maxWaitMs);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSocket__lastErrorText(socket));
    Exit;
  end;

//  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.
messageLength := 512;
success := CkSocket_SendCount(socket,messageLength);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSocket__lastErrorText(socket));
    Exit;
  end;
Memo1.Lines.Add('Length prefix sent.');

CkSocket_Dispose(socket);