Skip navigation
Currently Being Moderated

FTP connection on mobile device works over wifi but fails over 3G

Mar 15, 2012 8:00 PM

Tags: #problem #flash_builder #android #mobile #ios #actionscript #air3 #flex_mobile #4.6

Hi all,


Hoping someone can help, or has come across this issue.


I am developing a mobile app using flash builder 4.6 with the flex 4.6 sdk. My app needs to transfer files via ftp.


The issue I am experiencing is that the ftp connection establishes and files transfer without a problem when connected via wifi, on both android and ios platforms.


However, when only connecting via a 3G connection on a device, a problem occurs. While I can establish a connection, when I send the USER command, I never receive a response.


A simple view with code to reproduce this issue is listed below. Has anyone come across this problem? I have been searching far and wide but cannot find anything related.


Could this be an issue with Adobe Air or the socket class? I have written equivalent code using the android sdk in eclipse, and a connection to the same server and user details works on both wifi and 3g connections.


Any help would be appreciated.




<?xml version="1.0" encoding="utf-8"?>

<s:View xmlns:fx=""

                    xmlns:s="library://" title="HomeView">




                              private var commands:Array;

                              private var s:Socket;

                              private var data_channel:Socket;


                              private function connectToFtp():void


                                        var ftp_response:String;

                                        var data_response:String;

                                        var data_channel_ip:String;

                                        var data_channel_port:int;


                                        s = new Socket();

                                        s.addEventListener(Event.CLOSE, closeConnection);

                                        s.addEventListener(Event.CONNECT, connectionMade);

                                        s.addEventListener(IOErrorEvent.IO_ERROR, showError);

                                        s.addEventListener(SecurityErrorEvent.SECURITY_ER ROR, showSecError);

                                        s.addEventListener(ProgressEvent.SOCKET_DATA, receiveReply);

                                        s.connect("", 21);


                                        function receiveReply(e:ProgressEvent):void{



                                                  ftp_response = s.readUTFBytes(s.bytesAvailable);



                                                  var code:String = ftp_response.substr(0, 3);

                                                  if (code == "220")


                                                            // connected.

                                                            // send user command

                                                             s.writeUTFBytes("USER myUserName\n");



                                                  else if (code == "331")


                                                            // user command sent. password required

                                                            // send password

                                                            s.writeUTFBytes("PASS myPassword\n");



                                                  else if (code == "230")


                                                            // login successful

                                                            // change directory

                                                            s.writeUTFBytes("CWD /fibreSyncFtp/\n");



                                                  else if (code == "250")


                                                            // change directory successful

                                                            // send passive command




                                                  else if(code == "227")



                                                            //get the ip from the string response

                                                            var temp:String = ftp_response.substring(ftp_response.indexOf("(") + 1, ftp_response.indexOf(")"));

                                                            var data_channel_temp:Array = temp.split(",");


                                                            data_channel_ip = data_channel_temp.slice(0,4).join(".");


                                                            //calculate the port number from the last two digits - if this makes no sense, google

                                                            // FTP PROTOCOL - there are loads of guides that explain the server responses.

                                                            data_channel_port = parseInt(data_channel_temp[4]) * 256 + int(data_channel_temp[5]);



                                                            data_channel = new Socket(data_channel_ip, data_channel_port);

                                                            data_channel.addEventListener(ProgressE vent.SOCKET_DATA, receiveData);










                                                  else if (code == "125")


                                                            //use the new IP to open a second socket - this will transmit the data.

                                                            // Your first socket will transmit any commands you issue and this new socket will transmit the data


                                                  else if (code == "226")


                                                            // transfer complete

                                                            // close the connection



                                                  else if (code == "425")


                                                            // can't open connection



                                        function connectionMade(e:Event):void


                                                  trace("Connection Made");


                                        function receiveData(e:ProgressEvent):void{

                                                  data_response = data_channel.readUTFBytes(data_channel.bytesAvailable);



                                        function showError(e:IOErrorEvent):void{



                                        function showSecError(e:SecurityErrorEvent):void{




                                        function closeConnection(e:Event):void{







                    <!-- Place non-visual elements (e.g., services, value objects) here -->


          <s:Button top="10" label="FTP" click="connectToFtp()" horizontalCenter="0"/>


  • Currently Being Moderated
    May 4, 2013 4:56 AM   in reply to Bixy75

    Probably you need to write your own AIR native extension, which uses the native Android sockets..I have the same problem and I'm just starting writing the extension...I guess there is no other way..At least you test with native android socket and you confirm is fine, so we know it should work

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points