Android Question Using FTP Service to download a SQLLITE DB into the standard location on the mobile

beelze69

Active Member
Licensed User
Longtime User
Hi,

Can anyone provide a simple working example of how to connect to an i) FTP Server ii) Download a File from the Server iii) Ensure complete download of the file with facility to retry the download if it fails midway ... I have set up the FTP Service on my PC... Also please tell me where should I download an SQLLITE database onto my mobile (I need the full path where the SQLLITE DB normally resides in the mobile when it is bundled with an application package created via the B4Android IDE) ..
 

beelze69

Active Member
Licensed User
Longtime User
Hi !

Ps. discard last post of 1231 AM today .. rewording it properly as below..

I attempted to modify the code in the above link with mine... I am intending to download mydatabase.db which is in the home directory of my folder. I have FILEZILLA server running.. userid and password are belze and belze123 .. I am using the following command in the
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
FTP.Initialize( "FTP", "belze.duckdns.org", 21, "belze", "belze123")
End If
Activity.LoadLayout("Main")
End Sub

In some click event , I am typing

FTP.DownloadFile( "myDatabase.db", False, File.DirRootExternal, "myDatabase.db") //I wish to download a file myDatabase.db and name it to myDatabase.db

..but getting following error:
:BEGIN ERROR:
libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
:END ERROR:
But can FTP Directory via command line and get the file . .. Here is the trace ...
myPC>ftp belze.duckdns.org
Connected to belze.duckdns.org.
220-FileZilla Server 0.9.60 beta
220 Ftp Service of belze
User (belze.duckdns.org:(none)): belze
331 Password required for belze
Password:
230 Logged on
ftp> hash on
Hash mark printing On ftp: (2048 bytes/hash mark) .
ftp> mget mydatabase.db
200 Type set to A
mget myDatabase.db? y
200 Port command successful
150 Opening data channel for file download from server of "/myDatabase.db"
###################################################################226 Successfu
lly transferred "/myDatabase.db"
ftp: 137216 bytes received in 0.00Seconds 45738.67Kbytes/sec.
.Can you tell me where I am going wrong with the Code Part ? ....Please Help...
 
Upvote 0

beelze69

Active Member
Licensed User
Longtime User
Hi,

I tried hard-coding the IP .... I tried with both EXTERNAL as well as my INTERNAL 192.168.1.100 .. but it is giving TIMEOUT..
[LOG]
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
myDatabase.db, Success=false
libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
** Activity (main) Pause, UserClosed = false **
[/LOG]
I am attaching the main code below:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        FTP.Initialize(   "FTP",   "117.204.169.98", 21,  "belze", "belze123")
    End If
    Activity.LoadLayout("Main")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Msgbox("Firing the click event for download","FTP EXAMPLE") 
    FTP.DownloadFile( "myDatabase.db",  False, File.DirRootExternal,  "myDatabase.db")
End Sub

Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
    Dim s As String
    s = "Downloaded " & Round(TotalDownloaded / 1000) & "KB"
    If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
    Log(s)
End Sub

Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    Log(ServerPath & ", Success=" & Success)
    If Success = False Then Log(LastException.Message)
End Sub

Ps. help..
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
It looks like you do not have port forwarding setup on your modem/router. This modem/router needs to be configured to forward port 21 to port 21 and the internal IP address of the computer that is hosting your FileZilla FTP server. This is very modem/router specific and you just need to do a web search for that option (or just play around in the menu of your modem/router and look for port forwarding).

Warning(s):

  1. It looks like you have the management interface of your modem exposed to the public side of the network. Even though you have it "guarded" with a username/password, it is not recommended to expose the interface. Weaknesses of the code behind the management interface can be exploited, even without login in.
  2. Please don't give out a working IP address / domain name with a potential working username / password for one of your services unless you intend for others to use your service or to us the service as a stepping stone into your system (via an exploit).
You've pretty much given a hacker all the information they need (IP addresses - both external AND internal, username, password, type of software used, exposure of modem management interface, type of modem used, etc.) to have some fun lobbing exploit code at your network/system.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I tried with both EXTERNAL as well as my INTERNAL 192.168.1.100
For there to actually be an issue with your internal address, you have to ensure that both the PC (192.168.1.100) and the phone are on the same network (the phone must have an IP address of 192.168.1.x). You must also ensure that your PC is not blocking/firewalling off access to Port 21. If both of these premises are met (same network, firewall allowing access) and the phone still does not connect, then you may (as long as everything is done right on the networking/firewall side) have a B4A issue. Otherwise, it's all down to network/firewall configuration. Once you get that working, then you can work on trying to access the PC from the outside. At that time, the phone should not use your internal network, but be connected to the internet via cellular. A failure to connect could then be a missing/misconfigured port forwarding rule, or your ISP blocking FTP traffic to your public IP address (some providers frown on you hosting services on a residential plan versus a business plan. I don't know if BSNL does this), or some other networking issue. Please note that you accessing your FTP server that is hosted on your PC from your PC does not test any of these premises.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Try it and see. It will not hurt.
Even a ping does not work for this ip

ping 117.204.169.98

Ping wird ausgeführt für 117.204.169.98 mit 32 Bytes Daten:
Zeitüberschreitung der Anforderung.
Zeitüberschreitung der Anforderung.
Zeitüberschreitung der Anforderung.
Zeitüberschreitung der Anforderung.

Ping-Statistik für 117.204.169.98:
Pakete: Gesendet = 4, Empfangen = 0, Verloren = 4
(100% Verlust),
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Even a ping does not work for this ip
It worked last night. It could be that 1) he increased the security of the modem/router or 2) it is currently offline.

I always forget about passive mode...
 
Upvote 0
Top