(Java) Check SSH Connection (IsConnected)
Demonstrates how to check to see if the connection to the SSH server exists.
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
CkSsh ssh = new CkSsh();
// Connect to an SSH server:
boolean success = ssh.Connect("192.168.1.209",22);
if (success != true) {
System.out.println(ssh.lastErrorText());
return;
}
// Is the last known state "connected"?
boolean connected = ssh.get_IsConnected();
if (connected == true) {
// Verify for sure by sending an ignore message.
connected = ssh.SendIgnore();
}
System.out.println("connected = " + connected);
// Disconnect.
ssh.Disconnect();
// Am I still connected?
connected = ssh.get_IsConnected();
System.out.println("connected = " + connected);
}
}
|