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

Connect to an FTP Server Without Logging In

See more FTP Examples

Demonstrates the Chilkat Ftp2.ConnectOnly method, which establishes the connection (including any proxy and TLS setup) and receives the greeting, but does not send USER or PASS. It takes no arguments; authentication is completed with LoginAfterConnectOnly.

Background: Splitting connect from login gives you a window between the two — to read the greeting, decide which account to use, or set up TLS options that depend on the server — that the all-in-one Connect does not. After ConnectOnly the control connection is open and CheckConnection returns true even though the session is not yet authenticated.

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.ConnectOnly method, which establishes the connection (including any proxy
  //  and TLS setup) and receives the greeting, but does NOT send USER or PASS.  It takes no
  //  arguments.  Authentication is completed separately with LoginAfterConnectOnly.

  ftp := TFtp2.Create;

  ftp.Hostname := 'ftp.example.com';
  ftp.Port := 21;

  //  Connect without logging in.
  success := ftp.ConnectOnly();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;
  WriteLn('Connected (not yet logged in).');

  //  The greeting is now available; credentials can be set based on it, then authenticate.
  ftp.Username := 'myFtpLogin';
  ftp.Password := 'myPassword';

  success := ftp.LoginAfterConnectOnly();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;
  WriteLn('Logged in.');

  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.