Cellular Connection but no FTP

rfresh

Well-Known Member
Licensed User
Longtime User
I have a Droid3 with a cellular connection to the internet. I can browse to any website so I know I have an internet connection. I wrote a small FTP app that connects to my website and downloads a small text file.

If I have my Droid3 WiFi turned on, the FTP app works. If I turn off the Droid3 WiFi, the FTP app does not work (does not download the file).

I thought if I turned off the WiFi the FTP would connect thru the cellular data connection and download the file, but this is not happening.

Any idea's or suggestions appreciated...thanks...
 

kanaida

Active Member
Licensed User
Longtime User
Do you get some sort of socket error or server not responding?

Sometimes traffic may be hampered by transparent proxies and traffic shaping. ftp may be blocked at worst.

Did you try passive mode too? that seems to help sometimes.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
No, I didn't get any socket errors. The FTP trys to connect and seems to just hang. I'll try again and let it sit there and see if I get a socket error.

How do I use passive mode? Is that needed if I can connect via WiFi?

Remember, I can connect FTP using my phones WiFi.
 
Upvote 0

kanaida

Active Member
Licensed User
Longtime User
No, I didn't get any socket errors. The FTP trys to connect and seems to just hang. I'll try again and let it sit there and see if I get a socket error.

How do I use passive mode? Is that needed if I can connect via WiFi?

Remember, I can connect FTP using my phones WiFi.

Passive just tends to work more often through limited networks. It makes you open ports as opposed to the server sort of. It's actually a lot more complicated than it needs to be :) Are you using the ftp library? there may be a parameter or property.

Wifi is a different subnet than your phone so the network firewalls etc... could be completely different. That's why one would work and not the other.

What you want to see is an ftp log to see where it died if anything.

On a windows pc, click start, run, type cmd.
In the command prompt type:
ftp ftp.symantec.com (or your own ftp)
login as usual
type:
cd /thepathtothefile
get filename
bye

that will show you what normal communications look like. Then compare with your own log in your android app (hopefully it's in there somewhere). maybe you'll have to go to the b4a log tab on the bottom right and click connect to see it.
 
Last edited:
Upvote 0

kanaida

Active Member
Licensed User
Longtime User
The Net 1.2 library seems to have what you need to test:

B4X:
Dim f As FTP
f.Initialize("YourEventToHandleCompleted","ftp.something.com",21,User,Pass)
f.PassiveMode = True
f.DownloadFile("ServerFilePath",TrueIfItsATextFile,LocalFolder,LocalFileName)

DownloadCompleted (ServerPath As String, Success As Boolean)
DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
UploadCompleted (ServerPath As String, Success As Boolean)
UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
DeleteCompleted (ServerPath As String, Success As Boolean)
CommandCompleted (Command As String, Success As Boolean, ReplyCode As Int, ReplyString As String)
ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
 
Last edited:
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
So I got the FTP working now on my Droid3 phone (thanks to both of you).

I went into my settings and turned off the WiFi. I'm using the ToggleLibrary to detect both wifi and the data connection. It correctly shows wifi has dropped off and the data connection is still there (as expected).

However, this change of events now prevents the FTP from being able to download. It tries to download (out of the service module) but never completes it. Do I have to reinitialize again or is there something else I can try? I'm initializing in service create.

Update: I got the FTP to start working again by reinitializing the FTP in the code. My ServiceStartAt is every 5 mins and I set a flag to detect that the download completes or not. When Service comes back around in its cycle, if the last download didn't complete (it's a very small text file so it normally completes within a few secs) I check the download complete flag and if it didn't complete, that's when I do a reinitialize.

So further digging into this problem reveals why this happens: when the initial FTP connection is made, an OS handle is associated with the FTP session. Much like a handle being returned when you open a file. When you manually disconnect the wifi, the handle is no longer able to point to that wifi session. So you have to .CloseNow and then re-initialize the FTP session to the only remaining internet connection you have (on a phone at least) which is the DataConnection. Then the FTP will work with that connection.

So for anyone using FTP in your app, you need to be aware of this important possibility: on a phone with both a wifi and cellular connection to the internet, if the user manually turns off their phone's wifi, your FTP will no longer work until you issue a re-initialize statement to connect to the data connection.
 
Last edited:
Upvote 0

kanaida

Active Member
Licensed User
Longtime User
So I got the FTP working now on my Droid3 phone (thanks to both of you).

I went into my settings and turned off the WiFi. I'm using the ToggleLibrary to detect both wifi and the data connection. It correctly shows wifi has dropped off and the data connection is still there (as expected).

However, this change of events now prevents the FTP from being able to download. It tries to download (out of the service module) but never completes it. Do I have to reinitialize again or is there something else I can try? I'm initializing in service create.

Update: I got the FTP to start working again by reinitializing the FTP in the code. My ServiceStartAt is every 5 mins and I set a flag to detect that the download completes or not. When Service comes back around in its cycle, if the last download didn't complete (it's a very small text file so it normally completes within a few secs) I check the download complete flag and if it didn't complete, that's when I do a reinitialize.

So further digging into this problem reveals why this happens: when the initial FTP connection is made, an OS handle is associated with the FTP session. Much like a handle being returned when you open a file. When you manually disconnect the wifi, the handle is no longer able to point to that wifi session. So you have to .CloseNow and then re-initialize the FTP session to the only remaining internet connection you have (on a phone at least) which is the DataConnection. Then the FTP will work with that connection.

So for anyone using FTP in your app, you need to be aware of this important possibility: on a phone with both a wifi and cellular connection to the internet, if the user manually turns off their phone's wifi, your FTP will no longer work until you issue a re-initialize statement to connect to the data connection.

Yup. That and when you disable enable/wifi your ip might change from 192.168.1.100 -> 64.21.123.12 (examples). So your other end is now responding to an ip that does not = you halfway through a transfer. Some ftp's allow for resume commands etc... so if data receive times out, I roll back a few bytes and attempt to re-download from that offset -> EOF. This mostly only happens with bigger files anyways tho.

Lesson learned: Use an http server instead :) it's not a hassle, easier to configure, supports credentials, encryption if you enable SSL, can do everything that FTP can do, and comes built into every windows installation (install IIS windows component).
 
Upvote 0
Top