Sample code for 30+ languages & platforms
Android™

Close an SSH Tunnel

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshCloseTunnel, which closes the SSH tunnel opened by SshOpenTunnel and terminates its forwarded channels.

Background. Closing the tunnel does not affect unrelated SSH objects or connections.

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;

    CkSocket socket = new CkSocket();

    //  Connect to the SSH server and initialize an SSH tunneling session.  Port 22 is conventional but
    //  not required.
    success = socket.SshOpenTunnel("ssh.example.com",22);
    if (success == false) {
        Log.i(TAG, socket.lastErrorText());
        return;
        }

    //  Authenticate the SSH session before opening forwarded channels.
    //  The SSH password should come from a secure source rather than being hard-coded.
    String sshPassword = "mySshPassword";
    success = socket.SshAuthenticatePw("sshUser",sshPassword);
    if (success == false) {
        Log.i(TAG, socket.lastErrorText());
        return;
        }

    //  Close the SSH tunnel and terminate its forwarded channels.  This does not affect unrelated SSH
    //  objects or connections.
    success = socket.SshCloseTunnel();
    if (success == false) {
        Log.i(TAG, socket.lastErrorText());
        return;
        }

    Log.i(TAG, "SSH tunnel closed.");

  }

  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."
  }
}