android could not connect to ftp host - android

i am using the ftpclient library and followed their example to make a simple ftp client app, my problem is when trying to connect :
try {
mFTPClient = new FTPClient();
// connecting to the host
mFTPClient.connect(host, port);
}
catch (Exception e)
{
Log.d("", "Error: could not connect to host " + host);
}
i rechecked my ip and port and nothing wrong, i even downloaded an ftp app "ftpcafe" to check if the problem wasnt from my code it worked fine, * note that i added the
<uses-permission android:name="android.permission.INTERNET"/>
do i need more permissions? and what is going on? and one last thing when i used ftpcafe app it didnt prompt me to enter port number while its is needed in my code, can someone explain this as i am new to network programming thing?

Did you try without port (if port 21).
mFTPClient.connect(host);
You may have some information about the problem in the exception ?
Log.d("",e.getMessage());

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 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.

Kryonet disconnects client 1 as soon as client 2 connects

I've worked before with Kryonet doing 1-to-1 communication and it worked nicely.
Now I'm doing a more 'standard' project where we'll have 1 server and several clients to connect to it.
The issue I'm having is as described in the title: client 1 connects, no problem. Then I ask for client 2 to connect and immediately client 1 disconnects. Somehow the server doesn't want to keep more than one simultaneous connection.
A couple of times we managed to have 2 connected and then whenever the 3rd connects the other drops.
Running on several different devices all ICS+ (galaxy nexus, tab2, SGS3).
The codes I'm using are very much like the examples:
server side:
server = new Server();
ServiceData.RegisterKryo(server.getKryo());
server.addListener(new MyServerListener());
try {
server.bind(ServiceData.SERVER_PORT_TCP);
server.start();
} catch (IOException e) {
Log.e(TAG, "IOException. Failed to start server. " + e.getMessage());
MyServer.this.stopSelf();
}
And then client side:
final String ip = intent.getExtras().getString(KEY_SERVER_IP);
listener = new MyClientListener();
client = new Client();
client.start();
ServiceData.RegisterKryo(client.getKryo());
client.addListener(listener);
try {
client.connect(5000, ip, ServiceData.SERVER_PORT_TCP);
} catch (IOException e) {
Log.e(TAG, "IOException. Failed to start client. " + e.getMessage() + "\n");
e.printStackTrace();
MyClient.this.stopSelf();
}
the listeners at the moment are just Log.v(TAG, "something happened); and I've also enabled all the logs from the Kryonet library with com.esotericsoftware.minlog.Log.set(com.esotericsoftware.minlog.Log.LEVEL_TRACE); so I can see when it's connecting and when it's disconnecting.
I receive two different messages upon disconnection:
DEBUG: [kryonet] Connection 3 timed out.
and
DEBUG: [kryonet] Unable to read TCP from:
really not sure what's on here and any help will be appreciated.
edit:
a bit more info:
I've realised that between INFO: [kryonet] Connection 3 connected: /192.168.0.104 and my listener receive the connected callback, it's taking around 9 seconds! Very odd.
To whom might get into the same issue.
Apparently it's an Android limitation (probably imposed because it's a mobile device)
I just moved the Server code to a normal Java application .jar and let the Android clients connect to the PC and it all works fine now. Until now tested with 6 devices connected with no problems.

Android Bluetooth Connection Secure Insecure

I have been playing around with the bluetooth API for Android 2.2 (API level 8, HTC Desire) and had an app connecting to an embedded Bluetooth device using:
device.createRfcommSocketToServiceRecord(DEV_UUID);
This generated a pairing request as expected, however to streamline the connection process I wanted to avoid the user interaction when pairing so moved to API level 10 (HTC Desire with CyanogenMod 7) so I could use:
device.createInsecureRfcommSocketToServiceRecord(DEV_UUID);
When testing this also works as expected (connecting without prompting the user to pair), however when I try to create the secure RfcommSocket under API level 10 as before with 2.2 I get a connection refused exception...
java.io.IOException: Connection refused
at android.bluetooth.BluetoothSocket.connectNative(Native Method)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:204)
As far as I can tell this should still work in the same way, prompting the user to pair?
EDIT:
Just tried again using the following code and the outcome is the same (working for insecure but not for secure), I will try and get my hands on a stock 2.3 device to test on.
try {
Method m = dev.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class } );
BluetoothSocket bs = (BluetoothSocket)m.invoke(dev, devUUID);
Log.d("TEST", "Method Invoked");
bs.connect();
Log.d("TEST", "Connected to socket");
bs.close();
Log.d("TEST", "Closed Socket");
}
While looking for the solution of similar problem in my app, I have found this blog from code.google.com
It will help all those who are still looking for this problem solution on SO
http://mobisocial.stanford.edu/news/2011/03/bluetooth-reflection-and-legacy-nfc/ (link not working anymore)
The solution has become very simple now. Just include InsecureBluetooth.java in your project and change 2 lines in BluetoothChatService.java.
tmp = InsecureBluetooth.listenUsingRfcommWithServiceRecord(mAdapter, NAME, MY_UUID, true);
and
tmp = InsecureBluetooth.createRfcommSocketToServiceRecord(device, MY_UUID, true);
Thats it !

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