1 Reply Latest reply: May 4, 2013 4:56 AM by _flashman_ RSS

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

    Bixy75

      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.

       

      Thanks.

       

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

      <s:View xmlns:fx="http://ns.adobe.com/mxml/2009"

                          xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">

                <fx:Script>

                          <![CDATA[

       

                                    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_ERROR, showSecError);

                                              s.addEventListener(ProgressEvent.SOCKET_DATA, receiveReply);

                                              s.connect("mydomain.com", 21);

       

                                              function receiveReply(e:ProgressEvent):void{

       

       

                                                        ftp_response = s.readUTFBytes(s.bytesAvailable);

                                                        trace(ftp_response);

       

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

                                                        if (code == "220")

                                                        {

                                                                  // connected.

                                                                  // send user command

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

                                                                  s.flush();

                                                        }

                                                        else if (code == "331")

                                                        {

                                                                  // user command sent. password required

                                                                  // send password

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

                                                                  s.flush();

                                                        }

                                                        else if (code == "230")

                                                        {

                                                                  // login successful

                                                                  // change directory

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

                                                                  s.flush();

                                                        }

                                                        else if (code == "250")

                                                        {

                                                                  // change directory successful

                                                                  // send passive command

                                                                  s.writeUTFBytes("PASV\n");

                                                                  s.flush();

                                                        }

                                                        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(ProgressEvent.SOCKET_DATA, receiveData);

       

       

                                                                  s.writeUTFBytes("LIST\n");

                                                                  s.flush();

       

       

       

       

                                                        }

                                                        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

                                                                  //s.close();

                                                        }

                                                        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);

                                                        trace(data_response);

                                              }

                                              function showError(e:IOErrorEvent):void{

                                                        trace(e.text);

                                              }

                                              function showSecError(e:SecurityErrorEvent):void{

                                                        trace(e.text);

                                              }

       

                                              function closeConnection(e:Event):void{

                                                        trace(e.type);

                                              }

                                    }

                          ]]>

                </fx:Script>

                <fx:Declarations>

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

                </fx:Declarations>

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

      </s:View>