Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Receive Bytes as Encoded Text from a Socket

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveBytesENC, which receives one delivery of binary data and returns it encoded as text using a named encoding.

Background. The ENC methods move raw binary data to and from printable text using a named encoding such as base64 or hex, which is convenient for logging or for handling binary data as strings.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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;
  encoded: string;

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;

  //  Receive one delivery of binary data and return it encoded as text using the named encoding.  This
  //  is convenient for logging or transporting binary data as printable text.
  encoded := socket.ReceiveBytesENC('base64');
  if (socket.LastMethodSuccess = False) then
    begin
      WriteLn(socket.LastErrorText);
      Exit;
    end;
  WriteLn(encoded);


  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.