B4J Question [partially solved] FTP error ... when multiple ftp.list()

pierrem

Member
Licensed User
Longtime User
[3 edit]
maybe : " If Wait For is later called with the same event then the new sub instance will replace the previous one."
so :
I'll study : https://www.b4x.com/android/forum/threads/ftp-example-using-a-download-queue.39141/


[2 edit]
Need to declare in the sub the FTP object .... (and not once globally)

Next error : display only folders/files for the last call ...

Any ideas ?

thks




[1 edit]
Hi,

I get this error : "java.net.ConnectException: Connection timed out: connect" when using the following code


click function:
Private Sub btnCharge_Click
    Private nom As List
    nom.Initialize
    nom.Add("BOURGEAT")
    nom.Add("BRC")
    nom.Add("CODIGEL")
  
    'this code raise an error ...
    For Each n As String In nom
        download(n,"","")
    Next
  
    'only 1 call to download is OK
    'download(nom.Get(0),"","")
  
    'working too ...
    'download("","","")
End Sub

called function:
Sub download(f As String,p As String, nl As String)
    Private localPath As String = "/P6_FOURNISSEURS/"&f
    Log("localPath :"&localPath&" ("&p&"/"&nl&")")
    ftp.Initialize("ftp", "X.X.X.X", 21, "myUser", "myPSWD")
    'ftp.TimeoutMs = 100000
    'ftp.PassiveMode=True
    ftp.List(localPath)
       Wait For FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
       If Success Then
        Log("succes")
        Log("Dirs :")
        For Each dir As FTPEntry In Folders
            Log(dir.Name)
        Next
        Log("filess ")
        For Each file As FTPEntry In Files
            Log(file.Name)
        Next
        ftp.close
    Else
        Log("Erreur : "&ServerPath)
   End If
   Log("Finish")
End Sub

Obviously, the directories exist on the server ...
server : vsftpd, on Azure, no special config

I'm probably missing something ... dont understand what

Thanks in advance for your help !
 
Last edited:

Brian Dean

Well-Known Member
Licensed User
Longtime User
What happens when you set FTP.PassiveMode = True ? This is generally (almost always) the recommended setting.

By the way, although you can only run one download at a time on an FTP connection you can open more than one connection, but most servers will limit you to a maximum of four or five.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I have just looked at your code again and I can see a probable error, maybe two.
B4X:
    For Each n As String In nom
        download(n,"","")
    Next
"download()" is a resumeable sub, so that when execution hits the "Wait For ..." it will return, at which point "download(...)" gets called again. In "download(...)" you immediately reinitialise the (same?) FTP object, abandoning the previous instance. This is not good. You cannot run multiple jobs on a single FTP instance, and you cannot run jobs on multiple instances unless you keep control of their references.

Also this code appears to be inside a button click handler. This is also not good, as multiple click actions will try to start off repeated FTP list loops. You need an interlock to prevent that.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Here is some code that I think does what you are trying to do ...
B4X:
Sub Button1_Click
    ListFiles
    Do While busy                        ' Dim busy as Boolean = False as global variable ***
        Sleep(0)
    Loop
    Log(" ... continue")
End Sub

Sub ListFiles
    Dim nom As List
    nom.Initialize
    nom.Add("BOURGEAT")
    nom.Add("BRC")
    nom.Add("CODIGEL")
    
    Dim FTP As FTP
    FTP.Initialize("FTP", server, 21, user, password)
    FTP.PassiveMode = True
    busy = True
    For Each n As String In nom
        FTP.List(serverfolder & "/" & n & "/")
        Wait For FTP_ListCompleted (serverpath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
        If (Success) Then
            For Each f As FTPEntry In Files
                Log(f.Name)
            Next
        Else
            Log(LastException.Message)
        End If
    Next
    FTP.Close
    busy = False   
End Sub

Sorry - I did not have time to test this out earlier.
 
Upvote 0
Top