Android Question [SOLVED] Could SFtp expose JSch's Session.setTimeout()?

AscySoft

Active Member
Licensed User
Longtime User
Could SFtp expose JSch's Session.setTimeout()?

I am using the official B4A SFtp library for SFTP uploads over mobile connections.

In some cases an upload can remain blocked for around 2 minutes and eventually fail with:

ECONNABORTED

If I start the same upload again, it can succeed almost immediately, so I would like to use a shorter socket timeout and retry the operation with a fresh SFTP connection.

I tried accessing the underlying JSch Session using JavaObject, but SFtpWrapper does not expose a session field.

Would it be possible to add a public property or method such as:

SetTimeout(30000)

or:

TimeoutMs = 30000

which internally calls JSch:

session.setTimeout(timeout)

This would be useful for detecting stalled/broken connections earlier while still allowing normal slow transfers to continue as long as data is being transferred.

Thank you.
 

AscySoft

Active Member
Licensed User
Longtime User
I will test a workaraound solution, as below

The B4A SFtpWrapper does not expose a method for setting the connection timeout. With an unstable mobile connection, UploadFile may remain blocked for quite a long time before returning an error.

I inspected SFtpWrapper.class with javap and found the following private field:

Java:
private com.jcraft.jsch.Session session;
The connectIfNeeded() method eventually calls:

Java:
session.connect();
Therefore, the JSch timeout can be set after Initialize() but before UploadFile(), using Reflection and JavaObject to access the private session field:

B4X:
Try
Dim r As Reflector
r.Target = sftp
Dim cls As Object = r.RunMethod("getClass")
Dim rCls As Reflector
rCls.Target = cls
Dim field As Object = rCls.RunMethod2("getDeclaredField", "session", "java.lang.String")
Dim joField As JavaObject = field joField.RunMethod("setAccessible", Array(True))
Dim session As Object = joField.RunMethod("get", Array(sftp))
If session <> Null Then
Dim joSession As JavaObject = session joSession.RunMethod("setTimeout", Array(30000))
Log("SFTP timeout set to 30 seconds.")
End If
Catch
Log("Error setting SFTP timeout: " & LastException)
End Try

The important part is that this code runs before UploadFile(). Initialize() creates the JSch Session object, while the actual connection is established later by connectIfNeeded() when UploadFile() is called.

Tested successfully: the private Session object was retrieved and setTimeout(30000) was applied without errors.

This is a workaround rather than an official SFtpWrapper API, but it may be useful until a timeout property/method is exposed by the library.
 
Upvote 0
Top