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

Check if the FTP Control Connection is Open

See more FTP Examples

Demonstrates the Chilkat Ftp2.CheckConnection method, which returns whether Chilkat currently considers the FTP control connection to be open. It takes no arguments and does not require the session to be authenticated.

Background: This reports the object's view of the control-connection state, which is distinct from being logged in — it can return true after ConnectOnly and before authentication. For a stronger liveness check that the server is actually responding, send a Noop, which requires a positive reply. FTP servers commonly drop idle control connections after a timeout, so long-lived sessions should verify before assuming the connection is still usable.

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.Ftp2;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  ftp: TFtp2;

begin
  success := False;

  //  Demonstrates the Ftp2.CheckConnection method, which returns whether Chilkat currently considers
  //  the FTP control connection to be open.  It takes no arguments and does not require the session
  //  to be authenticated.

  ftp := TFtp2.Create;

  ftp.Hostname := 'ftp.example.com';
  ftp.Username := 'myFtpLogin';

  //  Normally you would not hard-code the password in source.  You should instead obtain it
  //  from an interactive prompt, environment variable, or a secrets vault.
  ftp.Password := 'myPassword';

  //  Connect and log in (Connect performs both).
  success := ftp.Connect();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;

  //  Check whether the control connection is still open.
  if (ftp.CheckConnection()) then
    begin
      WriteLn('The control connection is open.');
    end
  else
    begin
      WriteLn('The control connection is not open.');
    end;

  success := ftp.Disconnect();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;


  ftp.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.