Android™
Android™
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 Android™ Downloads
// 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.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.
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;
}
// ... clients connect and transfer data ...
// Disconnect all current clients. The listener keeps running and will accept new clients.
boolean waitForThreads = true;
success = tunnel.DisconnectAllClients(waitForThreads);
if (success == false) {
Log.i(TAG, tunnel.lastErrorText());
return;
}
Log.i(TAG, "All current clients disconnected; still accepting new ones.");
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."
}
}