Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkSshTunnel.pb"

Procedure ChilkatExample()

    success.i = 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.

    tunnel.i = CkSshTunnel::ckCreate()
    If tunnel.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSshTunnel::setCkDestHostname(tunnel, "db.internal.example.com")
    CkSshTunnel::setCkDestPort(tunnel, 5432)

    sshPort.i = 22
    success = CkSshTunnel::ckConnect(tunnel,"ssh.example.com",sshPort)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    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.
    password.s = "mySshPassword"

    success = CkSshTunnel::ckAuthenticatePw(tunnel,"mySshLogin",password)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    listenPort.i = 1080
    success = CkSshTunnel::ckBeginAccepting(tunnel,listenPort)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    ;  ... the tunnel runs for a while ...

    ;  Stop accepting NEW connections, but leave already-connected clients running.
    waitForThread.i = 1
    success = CkSshTunnel::ckStopAccepting(tunnel,waitForThread)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

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

    waitForThreadExit.i = 1
    success = CkSshTunnel::ckCloseTunnel(tunnel,waitForThreadExit)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf



    CkSshTunnel::ckDispose(tunnel)


    ProcedureReturn
EndProcedure