Android Question SSH Reconnection

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hi,
I am using jkSSH2 for SSH communications.
I have a apllication that connects to a host, retrieve some files and ...
when I change the hostaddress to reconnect to another host. It doesn't work
either I close the connection (ssh.close) or not it doesn't reconnect.
1) I Coneected to any host in my list and retrieve the files, then exit the application - All Ok
I close the connection and try to reconnect. I get errors downloading the files.
I dont close and simply change hosts, user and pass and re-initialize the class. I always get the files from the first host that tconnected.
Help would be greatly appreciated. I wouldnt like my users to have to quit and reenter the application each time they want to change hosts. Below are the 2 routines that initialize, download and close the communication.

B4X:
Sub SSH_Init
SSH.initialize("SSH", hAddress, 22)
SSH.authenticateWithPassword(hUsername, hPassword)
end Sub

Sub Download
SSH_Init
SSH.getFile ("/home/file2.txt",File.DirDefaultExternal ,12)
SSH.getFile ("/home/file1.txt",File.DirDefaultExternal ,12)
End Sub

Sub SSH_FileDownloadCompleted (Success As Boolean, RemoteFile As String, LocalTargetDirectory As String, TaskId As Int)
if Success = True then
SSh.Close
End if
End Sub
 
Last edited:

giga

Well-Known Member
Licensed User
Longtime User
Please use code insert when placing code in the forum.
example:

B4X:
Sub SSH_Init
SSH.initialize("SSH", hAddress, 22)
SSH.authenticateWithPassword(hUsername, hPassword)
end Sub

Sub Download

SSH_Init
SSH.getFile ("/home/file2.txt",File.DirDefaultExternal ,12)
SSH.getFile ("/home/file1.txt",File.DirDefaultExternal ,12)
End Sub

Sub SSH_FileDownloadCompleted (Success As Boolean, RemoteFile As String, LocalTargetDirectory As String, TaskId As Int)

if Success = True then
SSh.Close
End if
End Sub

Are you sure the first connection has completed and closed? Is the application closed?
 
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hello,
Yes I am sure. the SSH_Downloadcompleted event only triggers when the download process is finished, Successful or not.

I have a listview where the user select from a list of host.
The host, user, password values are beeing correctly passed to SSH_Init, because I have Already logged the vars to local window.
I am using the DisableStrictMode as shown below, as Erel suggested in a earlier post. Could be that, that is preventing the close / refresh of the SSH tunnel ?

B4X:
Sub DisableStrictMode
   Dim jo As JavaObject
   jo.InitializeStatic("android.os.Build.VERSION")
   If jo.GetField("SDK_INT") > 9 Then
     Dim policy As JavaObject
     policy = policy.InitializeNewInstance("android.os.StrictMode.ThreadPolicy.Builder", Null)
     policy = policy.RunMethodJO("permitAll", Null).RunMethodJO("build", Null)
     Dim sm As JavaObject
     sm.InitializeStatic("android.os.StrictMode").RunMethod("setThreadPolicy", Array(policy))
   End If
End Sub
 
Upvote 0
Top