Android Question FTP connect timeout setting

PumaCyan

Member
Licensed User
how to setup FTP connection / signal when FTP server is not active in FTP Client?

because in my code below, the process timed out for a very long time.
is that adjustable too?

My Code:
Private Sub B4XPage_Appear
    '
    ftp.Initialize("FTP", "192.168.1.100", "51041", "myuser", "mypassword")
    ftp.PassiveMode = True
    '
    Go_Download
    '
End Sub

Sub Go_Download
    PD.MyProgressDialogShow("Download File", "Please Wait...")
    '
    Dim strFile As String = "myfile.txt"
    Dim sf As Object
    sf = ftp.DownloadFile("download/" & strFile, False, xui.DefaultFolder, strFile)
    Wait For (sf) FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    If Success Then
        TM.MyToastMsg("Finish...")
        PD.MyProgressDialogHide
    Else
        TM.MyToastMsg("Error...")
        PD.MyProgressDialogHide
    End If
End Sub
 

vecino

Well-Known Member
Licensed User
Longtime User
If I remember correctly, you can specify the time you want, something like this:
B4X:
ftp.Timeout = 10000 ' ms
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
If I remember correctly ...
Not quite correct - the correct form is ...
B4X:
    FTP.TimeoutMs = 100
I use a commercial server and I have just tested it by reducing the timeout until I began to get connection errors. I always succeeded in uploading a file until I dropped the timeout to around 80msec, so using a setting of one or two hundred millisecs looks to be a quick and easy test.
 
Upvote 0

PumaCyan

Member
Licensed User
If I remember correctly, you can specify the time you want, something like this:
B4X:
ftp.Timeout = 10000 ' ms

so I put this code in the initialization section or during processing?
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
This is what I would do -

1. Before you send the real payload file set the FTP timeout to, say, 200msec.
2. Try to send a dummy test file, just a few bytes.
3. If the FTP link times out you can tell the User that they are not connected.
4. If the dummy file transfer succeeds reset the timeout to something sensible, say 5000, and send the real target file.

You can delete the dummy file at the host aftewards if you like, although that is not necessary
 
Upvote 0

PumaCyan

Member
Licensed User

how is the code implemented in the third point

My Code:
Sub FTP_Check
    ftp.Initialize("FTP", "192.168.1.100", 51047, "myuser", "mypassword")
    ftp.PassiveMode = True
    ftp.TimeoutMs = 100
    '
    Dim strFile As String = "dummy.txt"
    Dim sf As Object
    sf = ftp.DownloadFile("download/" & strFile, False, xui.DefaultFolder, strFile)
    Wait For (sf) FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    If Success Then
        Log("available...")
    Else
        Log("offline...")
    End If
End Sub


my code above, still the same
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
There are many ways this could be done. My preference would be to combine the server test and the file transfer into a single subroutine. I tried this out. It is easier to use B4J for testing so I wrote a class. You should be able to use this directly in B4A but you might prefer to adapt the scheme, or choose some other path. Note that I am using a different port number than you used in your post above. Here is the class ...
FTPLink Class:
Sub Class_Globals
    
    Private xui As XUI
    Private FTP As FTP
    Private dummyfile As String = "dummy.txt"        ' Used to test server link   
    
    Private host As String                                            ' Server location
    Private user As String                                            ' Account name
    Private pword As String                                            ' Password
    Private client As Object                                        ' Caller activity
    Private handler As String                                        ' Callback event handler
    
End Sub

Public Sub Initialize(hostname As String, username As String, password As String, caller As Object, eventHandler As String)
    host = hostname
    user = username
    pword = password
    client = caller
    handler = eventHandler
    If xui.IsB4J Then                                                        ' NOTE : Only needed if running on PC
        xui.SetDataFolder("Put appname here")
    End If
End Sub

' Download a file from FTP host after testing for active link
Public Sub getFile(hostfolder As String, filename As String)
    If Not(hostfolder.EndsWith("/")) Then hostfolder = hostfolder & "/"
    If Not(File.Exists(xui.DefaultFolder, dummyfile)) Then         ' Use short file to test for server
        File.WriteString(xui.DefaultFolder, dummyfile, "Test data")
    End If
    FTP.Initialize("FTP", host, 21, user, pword)
    FTP.PassiveMode = True
    FTP.TimeoutMs = 200                                                    ' Set timeout at lower limit
    Dim sf As Object                                                        ' Issue job to upload test file
    sf = FTP.uploadFile(xui.DefaultFolder & "\", dummyfile, True, hostfolder & dummyfile)
    Wait For (sf) FTP_uploadCompleted (ServerPath As String, Success As Boolean)
    If Not(Success) Then
        FTP.Close
        CallSub2(client, handler, "Host not available")
        Return   
    End If
    FTP.TimeoutMs = 5000                                                ' Server test succeeded : increase timeout
    sf = FTP.DownloadFile(hostfolder & filename, False, xui.DefaultFolder & "/", filename)
    Wait For (sf) FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    Dim msg As String
    If Success Then msg = "Transfer complete" Else msg = "Transfer failed"
    CallSub2(client, handler, msg)                            ' Pass result to callback handler
    FTP.Close
End Sub

Here is the code in the calling activity ...
B4X:
    . . .  
    Private FTPLink As FTPLink
    . . .
    FTPLink.Initialize("Server_name, "User_name", "Password", Me, "FTPstatus")    ' Define server and callback handler
    . . .

Private Sub btnTest_Click
    FTPLink.getFile("server folder name", "filename")
End Sub

Public Sub FTPstatus(Msg As String)
    Log("FTP : " & Msg)
End Sub

I tested for "server offline" by setting the FTP timeout to 20msec as I could not make my usual server inactive.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…