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

Receive a Block of Bytes to a File

See more Socket/SSL/TLS Examples

Demonstrates Socket.ReceiveBytesToFile, which receives one available block of bytes and appends it to a file.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. Any buffered data is written first; otherwise a single receive operation is performed. The method appends, so repeated calls accumulate received blocks in the file.

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;

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;

  //  The file path is relative to the application's current working directory.  An absolute path
  //  may also be used.  Supply the path appropriate to your own environment.

  //  Receive one available block of bytes and append it to a file.  If bytes are already buffered,
  //  they are used first; otherwise a single receive operation is performed.
  success := socket.ReceiveBytesToFile('qa_output/received.dat');
  if (success = False) then
    begin
      WriteLn(socket.LastErrorText);
      Exit;
    end;
  WriteLn('Received block appended to the file.');


  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.