Sample code for 30+ languages & platforms
Visual FoxPro

Stop an SSH Tunnel Listener (StopAccepting)

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.StopAccepting method, which stops the local listener so no new client connections are accepted. The only argument (waitForThread) selects whether to wait for the listener thread to exit. Existing forwarded clients remain connected.

Background: The distinction from CloseTunnel is the point: StopAccepting closes only the front door, leaving already-connected clients to finish their transfers undisturbed. That makes it the graceful way to drain a tunnel — stop taking new work, let existing work complete, then close — rather than cutting everyone off at once.

Chilkat Visual FoxPro Downloads

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

lnSuccess = 0

*  Demonstrates the SshTunnel.StopAccepting method, which stops the local listener so that no new
*  client connections are accepted.  The only argument (waitForThread) selects whether to wait for
*  the listener thread to exit.  Existing forwarded clients remain connected.

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 for a while ...

*  Stop accepting NEW connections, but leave already-connected clients running.
lnWaitForThread = 1
lnSuccess = loTunnel.StopAccepting(lnWaitForThread)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loTunnel
    CANCEL
ENDIF

? "The listener has stopped; existing clients continue."

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

RELEASE loTunnel