Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Receive a Single Byte from a Socket
See more Socket/SSL/TLS Examples
Demonstrates Socket.ReceiveByte, which reads exactly one byte and stores its numeric value in the ReceivedInt property. The argument selects signed or unsigned interpretation.
Background. Fixed-size integers and length prefixes are common building blocks for application-defined binary protocols. Byte order is selectable, with big-endian being network byte order.
Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Socket;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
socket: TSocket;
bTls: Boolean;
maxWaitMs: Integer;
bUnsigned: Boolean;
begin
success := False;
socket := TSocket.Create;
// Connect to the server using TLS.
bTls := True;
maxWaitMs := 5000;
success := socket.Connect('example.com',5000,bTls,maxWaitMs);
if (success = False) then
begin
WriteLn(socket.LastErrorText);
Exit;
end;
// Read exactly one byte. Its numeric value is stored in the ReceivedInt property. Passing True
// interprets the byte as unsigned (0 through 255); False interprets it as signed.
bUnsigned := True;
success := socket.ReceiveByte(bUnsigned);
if (success = False) then
begin
WriteLn(socket.LastErrorText);
Exit;
end;
WriteLn('Received byte value: ' + socket.ReceivedInt);
socket.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.