I am currently trying to develop an wearable app which can upload files with accelerometer and gyroscope data to my PC via FTP.
I used the following code to run a FTP server on my PC in Eclipse:
public static void main(String[] args) throws FtpException {
ListenerFactory listenerFactory = new ListenerFactory();
listenerFactory.setPort(2221);
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
UserManager userManager = userManagerFactory.createUserManager();
BaseUser user = new BaseUser();
user.setName("Anonymous");
user.setPassword("");
user.setHomeDirectory("C:\\temp");
List<Authority> authorities = new ArrayList<Authority>();
authorities.add(new WritePermission());
user.setAuthorities(authorities);
userManager.save(user);
FtpServerFactory factory = new FtpServerFactory();
factory.setUserManager(userManager);
factory.addListener("default", listenerFactory.createListener());
FtpServer server = factory.createServer();
server.start();
}
I am using this method to connect to the FTP server:
public void connectToFTPServer() throws IOException {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Log.i("CONNECT FTP SERVER", "Connecting to FTP Server");
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalActiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
}
And this method to upload the sensor data:
public void uploadFileTest()throws IOException {
try
{
if (ftpClient.login("Anonymous", ""))
{
String data = this.getFilesDir() + "/" + "SensorData.txt";
FileInputStream in = new FileInputStream(new File(data));
boolean result = ftpClient.storeFile("SensorData.txt", in);
in.close();
if (result) Log.v("upload result", "succeeded");
ftpClient.logout();
ftpClient.disconnect();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
The wearable is running on an emulator in Android Studio and even if it seems like the FTP server connection is established I experience the following error message on the FTP server when I try to upload the sensor data.
[main] INFO org.apache.ftpserver.impl.DefaultFtpServer - FTP server started
[NioProcessor-3] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - CREATED
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - OPENED
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 220 Service ready for new user.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: USER Anonymous
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 331 User name okay, need password for Anonymous.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: PASS *****
[pool-3-thread-1] INFO org.apache.ftpserver.command.impl.PASS - Login success - Anonymous
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 230 User logged in, proceed.
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: TYPE I
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 200 Command TYPE okay.
[NioProcessor-4] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - CREATED
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - OPENED
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 220 Service ready for new user.
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: USER Anonymous
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 331 User name okay, need password for Anonymous.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: PASS *****
[pool-3-thread-1] INFO org.apache.ftpserver.command.impl.PASS - Login success - Anonymous
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 230 User logged in, proceed.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: TYPE I
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 200 Command TYPE okay.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: PORT 10,0,2,15,157,216
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 200 Command PORT okay.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: STOR SensorData.txt
[pool-3-thread-1] WARN org.apache.ftpserver.impl.IODataConnectionFactory - FtpDataConnection.getDataSocket()
java.net.SocketException: Network is unreachable: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.ftpserver.impl.IODataConnectionFactory.createDataSocket(IODataConnectionFactory.java:312)
at org.apache.ftpserver.impl.IODataConnectionFactory.openConnection(IODataConnectionFactory.java:258)
at org.apache.ftpserver.command.impl.STOR.execute(STOR.java:133)
at org.apache.ftpserver.impl.DefaultFtpHandler.messageReceived(DefaultFtpHandler.java:211)
at org.apache.ftpserver.listener.nio.FtpHandlerAdapter.messageReceived(FtpHandlerAdapter.java:62)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$TailFilter.messageReceived(DefaultIoFilterChain.java:858)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:542)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1300(DefaultIoFilterChain.java:48)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:947)
at org.apache.ftpserver.listener.nio.FtpLoggingFilter.messageReceived(FtpLoggingFilter.java:85)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:542)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1300(DefaultIoFilterChain.java:48)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:947)
at org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:74)
at org.apache.mina.filter.logging.MdcInjectionFilter.filter(MdcInjectionFilter.java:135)
at org.apache.mina.filter.util.CommonEventFilter.messageReceived(CommonEventFilter.java:70)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:542)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1300(DefaultIoFilterChain.java:48)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:947)
at org.apache.mina.filter.codec.ProtocolCodecFilter$ProtocolDecoderOutputImpl.flush(ProtocolCodecFilter.java:398)
at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:234)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:542)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1300(DefaultIoFilterChain.java:48)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:947)
at org.apache.mina.core.filterchain.IoFilterEvent.fire(IoFilterEvent.java:74)
at org.apache.mina.core.session.IoEvent.run(IoEvent.java:63)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTask(OrderedThreadPoolExecutor.java:770)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.runTasks(OrderedThreadPoolExecutor.java:762)
at org.apache.mina.filter.executor.OrderedThreadPoolExecutor$Worker.run(OrderedThreadPoolExecutor.java:704)
at java.lang.Thread.run(Unknown Source)
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 150 File status okay; about to open data connection.
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 425 Can't open data connection.
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - CLOSED
Please guide me, I have looked for a possible solution for hours. I allowed the port in the windows firewall already and tried different other approaches.
Thank you all.
There's nearly zero chance that an active mode can work on a mobile/wearable device. FTP in an active mode requires enabling incoming connections to the devices, what in turn requires configuration of firewalls and NATs on a client side (or a client side network). Typically, an app on a mobile device is behind numerous such firewalls and NATs (both on the device itself, but mainly within the carriers network). And you have no (or little) control over these.
Use the passive mode instead.
For details, read my article on network configuration needed for FTP active and passive modes.
I'm aware that you commented, that passive mode does not work for you either. But that not's a reason to try the active mode. You should try to fix the passive mode (which is a way more likely to work), instead of wasting your time trying to fix an active mode. So consider posting a new question about the passive mode, with specific error messages and any other debugging information you have.