Problem when connecting to FTP server through android? - android

I've an UnknownHostException when i used this method for uploading files from ddms:
try {
SimpleFTP ftp = new SimpleFTP();
// Connect to an FTP server on port 21.
ftp.connect("ftp://*******", 21, "*****", "*****");
// Set binary mode.
ftp.bin();
// Change to a new working directory on the FTP server.
ftp.cwd("web");
// Upload some files.
ftp.stor(new File("data/data/com.android/file/contacts"));
// Quit from the FTP server.
ftp.disconnect();
}
catch (IOException e) {
// Jibble.
}
What is the problem for this method? Anyone clarify me.

There are two major problems with using FTP on the android emulator:
The emulator takes hold of a specific port on the host machine (between 5554 and 5584) to access the internet.
See http://developer.android.com/guide/developing/tools/emulator.html
FTP communicates on two ports. The initial port (the one the emulator is using) and a secondary data communication port (usually defined by the FTP client and server).
See http://www.troubleshootingnetworks.com/ftpinfo.html for information on how FTP works.
This means that the initial communication with the FTP server works the way it is intended, but once you are attempting to pass data to / from the server the emulator cannot communicate with the port the FTP server requests because your computer doesn't know what to do with the traffic on that port. See the link above to get a better grasp on FTP communications.
If you want to test FTP on Android you will need to have a device with its own internet connection.

Cursory look at SimpleFTP example suggests that you need to use host name without ftp:// prefix. Also make sure that you include INTERNET permission in manifest.

Related

Android FTP with 4G

I am trying to connect to an FTP server in an Android application and I believe that I fall in an infinite loop, but can't understand why. The library I am using is Apache Comomns net.
I have the internet permission in manifest :
<uses-permission android:name="android.permission.INTERNET"/>
I am also connecting to the FTP in an AsyncTask.
Here is a sample code which was supposed to work :
// I have other values when I test it of course
FTPClient ftpClient = new FTPClient();
ftpClient.connect("111.111.111.111"); // It stops working here when in 4G
ftpClient.login("user", "mdp");
ftpClient.changeWorkingDirectory("directory");
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.logout();
ftpClient.disconnect();
It works on Wifi (and my real application works too in wifi) but when I use my 4G (or 3G) it seems to loop at the line where I try to connect() forever.
I have tryed to understand what there is behind this but it's not clear for me.
How can I connect to my FTP using 4G?
Any help would be appreciated.
It sounds like your network operator may have the FTP port blocked.
To test this you can try to ftp to the same site on a browser in your mobile device - if it works then it point to an issue in your app.
If it does not work then try with a known working FTP site - Tele2 host a test site for example (there are lot of other open FTP sites if you do a quick search):
ftp://speedtest.tele2.net
If you can't access this from your mobile device over 4G then it looks very likely that your operator has FTP, and you should contact them to find out why and what you can do to remove the block.

Specify a network interface on Android

I've read the discussion, and now I am working on my own simple Android firewall. Here is the algorithm they used.
The discussion
I was able to forward packets. If I remember correctly I just needed to read the TCP header and open another socket to that destination and send it headerless.
So now I need to write data to my Wi-Fi or 3G network interface if I understand them correctly. How to do that?
I solved it. I only needed to protect the socket which I'd like to use to send data directly to the server without using the TUN device.
Socket socket = socket = SocketChannel.open().socket();
if (!protect(socket)) {
Log.e(Constants.TAG, "Failed to protect the socket");
}
The documentation

How to make the Android Device listen to a particular port number

I am new to android and is trying to develop an application. I have a local server that has the address like http://abc:9070/
i.e: the server is running only on port number 9070 in my laptop.
Now i want to debug my program using a android device and i have to make sure that the android device listens to port number 9070, So that i can make the post http request call to the url and fetch some information.
Can someone tell me how can i make my device to listen to port number 9070?
Also can someone tell me whether changing default port number of adb solve this.
I have tried a lot to search for a solution. But i am not able to come up with any good answers.
Thanks in advance.
Nobody has expressed an opinion yet. May be the question is not clear, at least I found it very difficult to understand what you are trying to do.
You say you have a server (laptop) listening on port 9070 and you want a device to connect to this server thru this port? Is that right?
Have you try, from your device, launch the navigator and connect to that address? http://abc:9070
Anyway, the java code to make a socket connection is something similar to this:
try
{
Socket clientSocket = new Socket("YOUR_LAPTOP_IP", 9070);
// 1024 is an arbitrary number, could be 512, 65535, etc
byte[] buffer = new byte[1024];
int ret=0;
while ((ret=clientSocket.getInputStream().read(buffer)) > 0)
{
// from now on it's up to you what to do with the data you read
}
clientSocket.close();
}
catch (Exception e)
{
e.printStackTrace();
}

Connecting to the ftp server on android gives issue

I have the android FTPserver app running on my android mobile under Tethering mode. I connected to my mobiles network from my laptop and tried to connect to mobile via FTP to access few files. I wrote a python code that uses the ftp connection to transfer files but I am unable to perform simple connection.
I keep getting this error
in windows:
server.gaierror: [Errno 11004] getadderinfo failed
in linux:
socket.gaierror: [Errno -2] Name or service not known
I searched so many places but never found a proper solution. All I could findout was people suggesting to check the firewall. I checked the firewall settings and everything and still I get the problem.
When I use a client software (filezilla) it does get connected.
I am a newbie to python could someone possibly tell me where I am going wrong?
here is my simple connecting code:
import os
from ftplib import FTP
ftp = FTP("192.168.43.1,5002")
ftp.login("usrid","pwd")
I am unable to connect to a general ftp test site, Here is the code for that:
from ftplib import FTP
ftp = FTP('ftp.cwi.nl')
ftp.login("anonymous","anonymous#")
ftp.retrlines('LIST')
do this:
ftp = FTP()
ftp.connect("192.168.43.1", 5002)
instead of:
ftp = FTP("192.168.43.1,5002")
because you can't give non-standard port number to the constructor.
Code:
from ftplib import FTP
ftp = FTP()
ftp.connect("192.168.43.1", 5002)
ftp.login("usrid", "pwd")
ftp.dir()
ftp.close()

Android FTP error - 425 Can't open data connection

I'm using this to upload some file. It works if I in a local connection, but if I use a external connection, i get this message: 425 Can't open data connection. from the ftp server.
I use the org.apache.commons.net.ftp.FTPClient and org.apache.commons.net.ftp.FTPFile libs.
public static String gravaImagem(String photoFile) {
FTPClient mFtp = new FTPClient();
try {
mFtp.connect(FTPHOST, PORTA);
mFtp.login(USUARIO, SENHA);
mFtp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
mFtp.setFileType(FTPClient.BINARY_FILE_TYPE);
String origem = Environment.getExternalStorageDirectory().getPath()+File.separator+"Pictures"+File.separator+"ImageSec"+File.separator+photoFile;
FileInputStream fis = new FileInputStream(origem);
mFtp.storeFile(photoFile, fis);
mFtp.logout();
mFtp.disconnect();
} catch (SocketException e) {
e.printStackTrace();
return "Fail. (ERR#CON3)";
} catch (IOException e) {
e.printStackTrace();
return "Fail. (ERR#CON4)";
}
return "Imagem enviada ao servidor.";
}
Debug shows no exceptions.
From the internet:
First - the most common solution: change the active/passive mode
settings. But that might not work, and if it does its only a band-aid
covering up the real problem.
As I've mentioned in the past, one of the most common reasons that
this error occurs is a misconfiguration of the FTP server software
itself, related to SSL connections and firewalls, in which the
connection tries to establish itself on a bogus ip address. Read more
about FTP SSL through a NAT firewall here, some potential solutions
are included.
There are other less likely causes, such as:
The server is configured to always use the same port for passive mode connections, or the client is configured to always use the
same port for active mode connections, although in this case
usually the software in question should raise a different error
first, but I've seen this happen.
In passive mode, the firewall in front of the FTP server doesn't have the correct ports open. So the server tells the client to
connect to ipaddress 1.2.3.4 on port x, but the firewall doesn't
allow incoming connections on port x. Most firewalls are smart
enough to open up the port when it sees the PASV response. Vice
versa for active mode and the firewall in front of the FTP client.
From me:
I've used this library on andoird and it worked well, so see my copy/paste section.

Categories

Resources