Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

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

lnSuccess = 0

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

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

*  ... clients connect and transfer data ...

*  Disconnect all current clients.  The listener keeps running and will accept new clients.
lnWaitForThreads = 1
lnSuccess = loTunnel.DisconnectAllClients(lnWaitForThreads)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loTunnel
    CANCEL
ENDIF

? "All current clients disconnected; still accepting new ones."

lnWaitForThreadExit = 1
lnSuccess = loTunnel.CloseTunnel(lnWaitForThreadExit)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loTunnel
    CANCEL
ENDIF

RELEASE loTunnel