Android FTP error - 425 Can't open data connection - android

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.

Related

Java server socket not accepting connection over tethered connection

I've written a small file transfer program for android using standard Java sockets. The program works fine except for the following case:
I connect two android devices A and B over WiFi tethering connection. Device A is sharing the connection (enabled as wireless hotspot). When I run java server on A and client on B, the program works okay but when I run the server on device B, it can't accept any socket binding request from A. It doesn't throw any exception. Seems like the request is not reaching the server! However, both the devices are connected (ping test is okay in both directions). Can't I run socket server on a device connected as hotspot client? I thought once the networking is setup correctly, the application would work in any direction.
Also, Wireshark traces reveal nothing. What am I missing here? Please help! Here are my code snippets:
Server side (waiting for client connection):
while (true) {
try {
socket = serversocket.accept();
Runnable connectionHandler = new ConnectionHandler(
socket, fileArray, filepathNameArray,
SenderActivity.this, userID, handler);
new Thread(connectionHandler).start();
userID = userID + 1;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I would appreciate any kind of help! Thanks in advance.

Android App Socket Creation (for Host Reachability) waits indefinitely while Web App works perfectly fine when connected via System Proxy

I am facing a tricky situation while working on a restricted network. Though I have my System Proxy Set to connect to my Web Server, Below are the two different behaviors observed from Web Browser and from Android Sockets .
1) With Proxy set, The Web Browser Request for the my server URL (host:port) goes well and I get 200 OK with valid Response Data from the Server.
2) With Android App, I do Host Reachability check before making my Connection Request. In Host reach-ability check , I create a java.net Socket and if it returns without any exception i consider my host as reachable. PSB for code snippet.
public static boolean isHostReachable(String hostname, int port) {
boolean isReachable = false;
Socket socket = null;
try {
socket = new Socket(hostname, port);
isReachable = true;
} catch (Exception e) {
.....
} finally {
....
}
return isReachable;
}
The whole logic of Host Reachability check works fine when in work from unrestricted network (lets say my home network).
The problem comes in the Host Reachability Socket Call , when I run my app from a restricted network (with Proxy Set to access my Server). Here my Socket Creation call does not return leading to host reach ability failure !!
There is a clear discrepancy between the browser and my app behavior in restricted network!!
My Question : While the Web Appllication works perfectly fine, What can be reason for the failure of above Socket Creation from Android leading to my Host Reachability failure in restricted network (with Proxy set in System Preference) and any suggestion for me to overcome this ?

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

Android TCP/IP Socket Communication Fails To Return Data

I am currently working on an Android app to be linked into an existing product for my employer. The system is a device running firmware that is controllable via serial, ethernet, or wi-fi using a .NET Windows program, direct serial communication, or a control webpage. The Android app is meant to auto-connect control webpage so that the device is controllable from a phone without anyone having to manually find IP addresses. In order to do that, I need to be able to find IP addresses and determine whether or not the address corresponds to one of these firmware devices.
Essentially what I want to do is run through all IP addresses that the Android sees and send each one to the method above. This thing should ping the address with a firmware command and see what is sent back to determine whether the address corresponds to a firmware device. I’ve been doing that at the moment by sending a command like "HOME" or "GETINFO" (correctly formatted for the firmware, of course) and comparing what, if anything is sent back with the expected response. Commands like home also have the benefit of causing a physical response, so we know without a microchip debugger if the command has at least been received.
As it stands I have the correct address hardcoded and sent in to this method. I know that the app can load the webpage controls successfully using the same address as a URL, so it must be correct and the connection must be good, but direct communication to the device is not working. No physical response is observed and no information is sent back—the Input stream just times out and returns -1. What’s stumping me so badly about this is that as far as I can tell, I’m sending information exactly the same way as the .NET Windows controls, and yet it isn’t working.
One further note: I’m aware that sending the IP Address string to the socket constructor as a hostname probably should not work, but since no UnknownHostException is thrown, I know that the socket can resolve it to an IP Address. Correct?
My code is as follows:
private class NetworkTask extends AsyncTask<String, Boolean, Boolean> {
protected Boolean doInBackground(String... addr){
try {
String message = "<FHGETHUBINFO>";
byte[] input = new byte[8];
//addr is an array of string parameters containing a single IP address string. E.g. addr[0]=”192.168.199.108”
Socket s = new Socket(addr[0],80);
//outgoing stream redirect to socket
OutputStream out = s.getOutputStream();
out.write(message.getBytes());
Log.v(TAG, "output sent. Waiting for input.");
InputStream in = s.getInputStream();
//Skip the exclamation mark and newline. Verified that nothing is received even without a skip.
in.skip(2);
int numBytes = in.read(input,0,8);
Log.v(TAG, "Input received: "+numBytes);
String st = input.toString();
//Close connection
s.close();
if(st != "HUB INFO"){
return true;
}
else{
return false;
}
}
catch (UnknownHostException e) {
Log.v(TAG,"UnknownHostException: "+e.getMessage());
e.printStackTrace();
}
catch (IOException e) {
Log.v(TAG,"IOException: "+e.getMessage());
e.printStackTrace();
}
return false;
Thanks for any help you can give, I really appreciate it!
Agreed that I should be calling isReachable on the socket just for verification purposes, so thanks for the tip! However, it turned out the problem was that the device is not communicating on port 80, so the fact that I have the wrong port is definitely the source of the problem. Thank you for the advice, regardless.

Problem when connecting to FTP server through 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.

Categories

Resources