Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
SSH Tunnel Timeouts and IP Preference
See more SSH Tunnel Examples
Demonstrates the ConnectTimeoutMs, IdleTimeoutMs, and PreferIpv6 properties, which govern how long the tunnel waits to connect and to transfer data, and which IP version it prefers.
Background:
ConnectTimeoutMs bounds how long a connection attempt blocks — important so an unreachable server fails promptly instead of hanging — while IdleTimeoutMs guards against a stalled transfer holding a connection open indefinitely. PreferIpv6 only matters when a hostname resolves to both address families, choosing which to try first; the default prefers IPv4.Chilkat Pascal (Lazarus/Delphi) Downloads
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;
waitForThreadExit: Boolean;
begin
success := False;
// Demonstrates the SshTunnel connection-behavior properties ConnectTimeoutMs, IdleTimeoutMs, and
// PreferIpv6.
tunnel := TSshTunnel.Create;
// Maximum time to establish the SSH connection (default 30000 ms).
tunnel.ConnectTimeoutMs := 10000;
// Stall timeout for tunnel data-transfer operations (default 30000 ms).
tunnel.IdleTimeoutMs := 15000;
// When a hostname resolves to both IPv4 and IPv6, prefer IPv6. The default (False) prefers IPv4.
tunnel.PreferIpv6 := True;
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;
WriteLn('Tunnel established.');
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.