iOS Question [solved] FTP download crashes web-server (B4i only - not B4a!)

Discussion in 'iOS Questions' started by yonson, Feb 9, 2015.

  1. yonson

    yonson Active Member Licensed User

    I'm using the same FTP routine to download a number of images from my webserver. I've displayed the relevant code below.

    The very strange thing is that the code is idential to the B4A implementation which I've been using for years. Yet on B4i, the download usually stop around 40-50 images in, and after that my website crashes. I have contacted the hosting company too to see if there is a reason for this, but I was wondering if anyone could explain what the differences are between android and iOS and a suggested workaround (e.g. I was thinking perhaps putting a delay in between downloads, but this is obviously creating extra inefficiency)

    Code:
    totalDownloads=0
      
        
    Dim thisFTP As FTP
        thisFTP.Initialize(
    "thisFTP",FTP_images_address, FTP_images_port, FTP_images_username, FTP_images_password)

            
    For i=0 To missingFileList.Size-1
                
    Dim thisFile As Map
                thisFile=missingFileList.Get(i)


                
    ' check we haven't already downloaded it (another point might be using it)
                If (MiscUtils.isFilePresent(thisFile.Get("directory")&"/"&thisFile.Get("filename")) = FalseThen
                    
    File.MakeDir(File.DirDocuments,thisFile.Get("directory"))
            
                  
                    
    ' create directory if it doesn't exist
                    Dim directoryPath As String
                    directoryPath = 
    File.DirDocuments & "/"&thisFile.Get("directory")
                    
    If File.Exists(directoryPath, "") = False Then File.MakeDir(File.DirDocuments,thisFile.Get("directory"))
                    thisFTP.DownloadFile(
    "./"&thisFile.Get("directory")&"/"&thisFile.Get("filename"),False,File.DirDocuments&"/"&thisFile.Get("directory"),thisFile.Get("filename"))  
                  
                
    Else
                    
    Log("File ALREADY DOWNLOADED ON THIS DOWNLOAD CYCLE")
                
    End If
              
            
    Next

    Sub thisFTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    ' great - move onto next one
    End Sub
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Add this code after you call thisFTP.Initialize:
    Code:
    Dim no As NativeObject = thisFTP
    no.GetField(
    "server").SetField("persistConnection"true)
    Does it change the behavior?
     
  3. yonson

    yonson Active Member Licensed User

    Hi Erel

    unfortunately that actually makes the problem worse - it causes the download time to freeze up almost completely (so to download a file of say 20Kbs takes around 60 seconds).
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Seems like your only solution is to add a delay of a few seconds between every several downloads.
     
  5. yonson

    yonson Active Member Licensed User

    Hi Erel
    thanks - tried that but it didn't work. One thought, is the FTP actually downloading files in sequence? It might be my lack of knowledge, but it appears that in B4i the ftp is attempting to download all the files at once, and this would explain why the server crashes.

    Could you have a look at the code and see if there is anything that jumps out which I could change to force sequential downloading (i.e. don't start next one until last one has finished)
    Thanks
    John
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    The requests are sent sequentially (unless you use multiple instances of FTP). However you can easily change your code to add a short delay between the downloads.

    Add all the file paths to a List. Download the first one.
    In the completed event remove the first item from the list and start a timer. When the timer ticks disable the timer and download the next file from the list.
     
  7. yonson

    yonson Active Member Licensed User

    Hi Erel

    many thanks for your help. I've tried a number of solutions to try and get to the root of this issue, but no matter what delay time I use it always falls over and causes the webserver to crash. It is very odd indeed!

    This is a slimmed down version of the code. I can't see anything which would cause the problem, and thoughts? As I say we have absolutely no issues in the equivalent B4A app.
    Thanks
    John



    Code:
    Dim missingFileList As List
    missingFileList = getListofMissingFiles
    thisFTP.Initialize(
    "thisFTP",FTP_images_address, FTP_images_port, FTP_images_username, FTP_images_password)
      
    ' tried using this - actually makes things worse
    'Dim no As NativeObject = thisFTP
      'no.GetField("server").SetField("persistConnection", True)

    downloadNextFile


    Sub downloadNextFile
        
    ' only continue if we have files left to download
        if (missingFileList.size<1then return
      
        
    Dim now As Long
    Dim ms as Long = 5000
       now=
    DateTime.now
       
    Do Until (DateTime.now>now+ms)
         
    ' dont' do anything
            'Log("waiting")
       Loop
        
    Log ("sleep ended at "&DateTime.now)

        
    Dim thisFile As Map
        thisFile=missingFileList.Get(
    0)
        
    If File.Exists(directoryPath, "") = False Then File.MakeDir(File.DirDocuments,thisFile.Get("directory"))
        
    Log("STARTING DOWNLOAD OF FILE "&thisFile.Get("filename"))
        thisFTP.downloadFile(
    "./"&thisFile.Get("directory")&"/"&thisFile.Get("filename"),False,File.DirDocuments&"/"&thisFile.Get("directory"),thisFile.Get("filename"))  
    End Sub

    Sub thisFTP_DownloadCompleted (ServerPath As String, Success As Boolean)
        
    ' remove the first element from the missingFileList, as this is the file we've removed
        missingFileList.RemoveAt(0)  
        downloadNextFile
    End Sub
     
    Last edited: Feb 26, 2015
  8. Erel

    Erel Administrator Staff Member Licensed User

    I don't see any delay here. Start a timer that will call downloadNextFile in 5000ms and then disable it.
     
  9. yonson

    yonson Active Member Licensed User

    Hi Erel

    thanks - I actually removed the delay because it didn't change the behaviour, i.e. it still crashed the app even when waiting 60 seconds between file downloads. I've put the delay back in to show you the code. Any thoughts?
    Thanks
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Does it crash the app or the ftp server?
     
  11. yonson

    yonson Active Member Licensed User

    the webserver crashes i.e. the website goes down completely. The app itself just 'hangs' i.e. the next download is never completed.

    If it would help I could prepare you a sample project with the relevant code and send it over, the web server is usually back online in about an hour.
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    You need to access the server logs and see the error. If you are only interested in downloading, then try to use HttpUtils2 to download the files from the http server.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice