Sample code for 30+ languages & platforms
Android™

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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

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

    CkSshTunnel tunnel = new CkSshTunnel();

    tunnel.put_DestHostname("db.internal.example.com");
    tunnel.put_DestPort(5432);

    int sshPort = 22;
    success = tunnel.Connect("ssh.example.com",sshPort);
    if (success == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }

    //  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.
    String password = "mySshPassword";

    success = tunnel.AuthenticatePw("mySshLogin",password);
    if (success == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }

    int listenPort = 1080;
    success = tunnel.BeginAccepting(listenPort);
    if (success == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }

    //  ... the tunnel runs for a while ...

    //  Stop accepting NEW connections, but leave already-connected clients running.
    boolean waitForThread = true;
    success = tunnel.StopAccepting(waitForThread);
    if (success == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }

    Log.i(TAG, "The listener has stopped; existing clients continue.");

    boolean waitForThreadExit = true;
    success = tunnel.CloseTunnel(waitForThreadExit);
    if (success == false) {
        Log.i(TAG, tunnel.lastErrorText());
        return;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}