Sample code for 30+ languages & platforms
Visual FoxPro

Close an SSH Tunnel (CloseTunnel)

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.CloseTunnel method, which disconnects active tunnel clients and stops the tunnel-manager thread. The only argument (waitForThreads) selects whether to wait for the worker threads to exit.

Background: This is the full shutdown — it stops the listener, disconnects clients, and ends the background threads, so it is what you call when the tunnel is no longer needed. Passing true for waitForThreads makes the call block until the workers have actually exited, which matters before the program terminates or reuses the object; if a worker fails to stop, the method returns false and LastErrorText explains why.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loTunnel
LOCAL lnSshPort
LOCAL lcPassword
LOCAL lnListenPort
LOCAL lnWaitForThreads

lnSuccess = 0

*  Demonstrates the SshTunnel.CloseTunnel method, which disconnects active tunnel clients and stops
*  the tunnel-manager thread.  The only argument (waitForThreads) selects whether to wait for the
*  worker threads to exit.

loTunnel = CreateObject('Chilkat.SshTunnel')

loTunnel.DestHostname = "db.internal.example.com"
loTunnel.DestPort = 5432

lnSshPort = 22
lnSuccess = loTunnel.Connect("ssh.example.com",lnSshPort)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loTunnel
    CANCEL
ENDIF

*  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.
lcPassword = "mySshPassword"

lnSuccess = loTunnel.AuthenticatePw("mySshLogin",lcPassword)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loTunnel
    CANCEL
ENDIF

lnListenPort = 1080
lnSuccess = loTunnel.BeginAccepting(lnListenPort)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loTunnel
    CANCEL
ENDIF

*  ... the tunnel runs ...

*  Shut everything down: disconnect clients and stop the tunnel-manager thread.  With
*  waitForThreads 1, the method waits for the worker threads to exit.
lnWaitForThreads = 1
lnSuccess = loTunnel.CloseTunnel(lnWaitForThreads)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loTunnel
    CANCEL
ENDIF

? "Tunnel closed."

RELEASE loTunnel