Delphi ActiveX
Delphi ActiveX
Check if the SSH Connection is Alive
See more SSH Examples
Demonstrates the Chilkat Ssh.CheckConnection method, which reports whether the underlying TCP socket still appears to be open. It takes no arguments and returns true if the socket does not appear closed, or false if the connection is known to be closed or invalid. This example connects, authenticates, checks the socket, and then uses SendIgnore for a definitive liveness test.
Background: Unlike the
IsConnected property, which simply reports the state the Ssh object already knows, CheckConnection performs an additional non-consuming check of the socket — it calls the operating system's recv with MSG_PEEK to inspect up to one byte without removing any data from the receive buffer. It is still a local check: it sends no SSH message, does not authenticate, and does not prove the remote server is responsive. A silently broken network path can keep it returning true until the OS notices, and a receive timeout, aborted operation, auth failure, or rejected channel-open does not necessarily close the socket. When you need an actual SSH round trip, use SendIgnore. Also note a false result may simply mean no connection exists, and this method may not update LastErrorText, so an existing error string could describe an earlier operation.Chilkat Delphi ActiveX Downloads
var
success: Integer;
ssh: TChilkatSsh;
sshPort: Integer;
password: WideString;
begin
success := 0;
// Demonstrates the Ssh.CheckConnection method, which reports whether the underlying TCP socket
// still appears to be open. It takes no arguments and returns 1 if the socket does not
// appear closed, or 0 if the connection is known to be closed or invalid.
//
// Unlike the IsConnected property, which reports the state already known to the Ssh object,
// CheckConnection performs an additional non-consuming peek at the socket. It is still a local
// check: it does not send an SSH message or prove the remote server is responsive. Use
// SendIgnore when an active SSH round trip is required.
ssh := TChilkatSsh.Create(Self);
sshPort := 22;
success := ssh.Connect('ssh.example.com',sshPort);
if (success = 0) then
begin
Memo1.Lines.Add(ssh.LastErrorText);
Exit;
end;
// 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.
password := 'mySshPassword';
success := ssh.AuthenticatePw('mySshLogin',password);
if (success = 0) then
begin
Memo1.Lines.Add(ssh.LastErrorText);
Exit;
end;
// Check whether the socket still appears open.
if (ssh.CheckConnection()) then
begin
Memo1.Lines.Add('The socket still appears open.');
end
else
begin
Memo1.Lines.Add('The connection is closed or invalid.');
end;
// Note: a silently broken network path can still report open here until the OS detects it.
// For a definitive test that the SSH connection is writable, send an IGNORE message.
success := ssh.SendIgnore();
if (success = 0) then
begin
Memo1.Lines.Add(ssh.LastErrorText);
Exit;
end;
Memo1.Lines.Add('SSH IGNORE sent -- the connection is writable.');
ssh.Disconnect();