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

Disconnect All SSH Tunnel Clients

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.DisconnectAllClients method, which disconnects all active tunnel clients while leaving the listener and SSH connection available. The only argument (waitForThreads) selects whether to wait for the client threads to exit.

Background: This is the complement of StopAccepting: it clears out current clients but keeps accepting new ones, so the listener and SSH transport stay up. It is useful for forcibly resetting stuck or unwanted connections without tearing down the whole tunnel — for example, dropping all sessions before a maintenance action while leaving the tunnel ready to serve fresh connections immediately afterward.

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

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

procedure RunDemo;
var
  success: Boolean;
  tunnel: TSshTunnel;
  sshPort: Integer;
  password: string;
  listenPort: Integer;
  waitForThreads: Boolean;
  waitForThreadExit: Boolean;

begin
  success := False;

  //  Demonstrates the SshTunnel.DisconnectAllClients method, which disconnects all active tunnel
  //  clients while leaving the listener and SSH connection available.  The only argument
  //  (waitForThreads) selects whether to wait for the client threads to exit.

  tunnel := TSshTunnel.Create;

  tunnel.DestHostname := 'db.internal.example.com';
  tunnel.DestPort := 5432;

  sshPort := 22;
  success := tunnel.Connect('ssh.example.com',sshPort);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;

  //  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.
  password := 'mySshPassword';

  success := tunnel.AuthenticatePw('mySshLogin',password);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;

  listenPort := 1080;
  success := tunnel.BeginAccepting(listenPort);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;

  //  ... clients connect and transfer data ...

  //  Disconnect all current clients.  The listener keeps running and will accept new clients.
  waitForThreads := True;
  success := tunnel.DisconnectAllClients(waitForThreads);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;
  WriteLn('All current clients disconnected; still accepting new ones.');

  waitForThreadExit := True;
  success := tunnel.CloseTunnel(waitForThreadExit);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;


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