Android™
Android™
Check the SSH Connection (IsConnected)
See more SSH Examples
Demonstrates how to check whether the connection to the SSH server still exists. The IsConnected property is checked first, and SendIgnore is then used to confirm the link for certain.
Background: These checks differ in strength.
IsConnected simply reports the state the object already knows and involves no network activity, so it can stay true after a silent network failure the operating system has not yet noticed. SendIgnore sends an SSH IGNORE message — discarded by the peer by design — which exercises the real write path and is therefore much stronger evidence the link is usable. CheckConnection sits between the two, peeking at the socket without sending anything.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 how to check whether the connection to the SSH server still exists.
CkSsh ssh = new CkSsh();
int port = 22;
success = ssh.Connect("ssh.example.com",port);
if (success == false) {
Log.i(TAG, ssh.lastErrorText());
return;
}
// IsConnected reports the state already known to the Ssh object. It does not perform a
// network round trip.
boolean connected = ssh.get_IsConnected();
if (connected) {
// Verify for certain by sending an SSH IGNORE message, which exercises the actual write
// path. The server sends no reply, but a successful send proves the link is writable.
connected = ssh.SendIgnore();
}
Log.i(TAG, "connected = " + String.valueOf(connected));
ssh.Disconnect();
// After disconnecting, the object reports that it is no longer connected.
connected = ssh.get_IsConnected();
Log.i(TAG, "connected = " + String.valueOf(connected));
}
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."
}
}