4 Replies Latest reply on Apr 27, 2012 8:07 AM by Flex FTP Project

    Error #2031: Socket Error. URL: xx.xx.xx.xx - Please help?

    Flex FTP Project

      Hi All,

       

      I posted my code recently as I am writing my first Flex/Adobe Air application to connect to an FTP server and upload a file. My initial issue was that I would connect to the FTP server but for some reason was not accepting my USER and PASS FTP commands, I therefore used the same code but pointed it to a different FTP server software (Filezilla server) and it now successfully connects.

       

      I then select a file from my desktop to upload into the root of the FTP location folder, the FTP server comes back with 150 Connection Accepted but then i get the socket error (screenshot attached).

       

      Can anyone out there please help me with this, I have been trying to fix it for 3 days now and I'm not getting anywhere. I heard it may be because of a Flex compiler setting "-use-network=false" but I have applied this change and there is no success in uploading the file.

       

      Here is my code - can you compile it and see if it works for you guys connecting to an FTP server of yours?? Any help would be VERY much appreciated:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" initialize="upLoad()"
              xmlns:s="library://ns.adobe.com/flex/spark"
              xmlns:mx="library://ns.adobe.com/flex/mx"
              height="665" title="FTP Utility">

       

      <fx:Script>

       

        <![CDATA[ 
       
        import mx.controls.Alert;
        import mx.utils.*; 
       
        private var fileRef:FileReference;
        private var fileSize:uint;
        private var fileContents:ByteArray;

        //you need to initiate two sockets one for sending commands and second for sending data to FTP Server socket for sending commands to FTP
        private var s:Socket = new Socket();
        
        //responce from FTP
        private var ftpResponse:String;
        private var serverResponse:Number;
       
        //socket for sending Data to FTP
        private var dataChannelSocket:Socket;
       
        //responce from FTP when sending Data to FTP
        private var dataResponse:String;
       
        //will hold the IP address of the new socket created by FTP
        private var dataChannelIP:String/*  = "127.0.0.1"; */
       
        //will hold the Port number created by FTP
        private var dataChannelPort:int/*  = 21; */
        
        private var user:String="TestFTP";//FTP username
        private var pass:String="**********";//FTP Password 
        
        
        private function receiveReply(e:ProgressEvent):void {
        
         ftpResponse = s.readUTFBytes(s.bytesAvailable)
        
         var serverResponse:Number = Number(ftpResponse.substr(0, 3));
         
          if(ftpResponse.indexOf("227")>-1) {
         
           //get the ip from the string response (all commented out as I have the IP and port hard coded for testing purposes
           var temp:Object = ftpResponse.substring(ftpResponse.indexOf("(")+1,ftpResponse.indexOf(")"));
           var dataChannelSocket_temp:Object = temp.split(",");
           dataChannelIP = dataChannelSocket_temp.slice(0,4).join(".");
            dataChannelPort = parseInt(dataChannelSocket_temp[4])*256+int(dataChannelSocket_temp[5]);
           trace("Data Channel socket is connected to IP: "+dataChannelIP);
           trace("Data channel socket is using port number: "+dataChannelPort);
       
           //create new Data Socket based on dataChannelSocket and dataChannelSocket port
           dataChannelSocket = new Socket(dataChannelIP,dataChannelPort);
           dataChannelSocket.addEventListener(ProgressEvent.SOCKET_DATA, receiveData);
          }
         
          //few FTP Response Codes
          switch(String(serverResponse)) {
           case "220" :
            //FTP Server ready responce
            traceData(ftpResponse);
          
           break;
         
           case "331" :
            //User name okay, need password
            traceData(ftpResponse);
           
           break;
         
           case  "230":
            //User  logged in
            traceData(ftpResponse);
           
           break;
          
           case "227" :
            //Entering Passive Mode
            traceData(ftpResponse);
           
           break;
          
           case "425" :
            //Data Connection Cannot be opened
            traceData(ftpResponse);
           
            break;
          
           case "150" :
            //File Status ok, about to open data connection
            traceData(ftpResponse);
           
            break;
          
           case "500" :
            //Syntax error, command unrecognised
            traceData(ftpResponse);
           
            break;
          
           case "503" :
            //Syntax error, command unrecognised
            traceData(ftpResponse);
           
            break;
          
           case "426" :
            //Syntax error, command unrecognised
            traceData(ftpResponse);
           
            break;
          
           case "421" :
            //Syntax error, command unrecognised
            traceData(ftpResponse);
           
            break;
          
           case "226" :
            //Syntax error, command unrecognised
            traceData(ftpResponse);
           
            break;
          
           case "200" :
            //Syntax error, command unrecognised
            traceData(ftpResponse);
           
            break;
          
            default:
          } 
        
        }
       
        private function receiveData(e:ProgressEvent):void {
       
         dataResponse = dataChannelSocket.readUTFBytes(dataChannelSocket.bytesAvailable);
         traceData("dataChannelSocket_response—>"+dataResponse);
        }
       
        private function showError(e:IOErrorEvent):void {
         traceData("Error—>"+e.text);
        }
       
        private function showSecError(e:SecurityErrorEvent):void {
         traceData("SecurityError–>"+e.text);
        } 
       
        private function createRemoteFile(fileName:String):void{
       
         if(fileName!=null && fileName !="") {
          s.writeUTFBytes("STOR "+fileName+"\n");
          s.flush();
         }
       
        }

       

        private function sendData():void {
        
         fileContents=fileRef.data as ByteArray;
         fileSize=fileRef.size;
        
         trace("Contents of file: "+fileContents);
         trace("Filesize is "+fileSize+"kb");

       

         dataChannelSocket = new Socket(dataChannelIP,dataChannelPort);
         /* trace("IP is:"+dataChannelIP);
         trace("Port is:"+dataChannelPort); */
         dataChannelSocket.addEventListener(ProgressEvent.SOCKET_DATA, receiveData);
         dataChannelSocket.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
         dataChannelSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
         dataChannelSocket.writeBytes(fileContents,0,fileSize);
         dataChannelSocket.flush();
        
        }
       
        private function upLoad():void {

       

         fileRef = new FileReference();
        
         //some eventlistener
         fileRef.addEventListener(Event.SELECT, selectEvent);
         fileRef.addEventListener(Event.OPEN, onFileOpen); 
        
         //this function connect to the ftp server
         connect();

       

         //send the username and password
         this.userName(user);
         this.passWord(pass);
         
         //if you want to change the directory for upload file
         //this.changeDirectory("Side project");//directory name
       
         //enter into PASSV Mode
         s.writeUTFBytes("PASV \n");
         s.flush();

        }
       
        private function onFileOpen(event:Event):void {
       
        } 
       
        private function traceData(event:Object):void {

       

         var tmp:String = "================================";
         ta.text +=event.toString();
         ta.verticalScrollPosition += 20;
        }

        private function ioErrorEvent(event:IOErrorEvent):void {

       

         Alert.show("IOError:" + event.text);

        }
        
        private function errorHandler(errorEvent:IOErrorEvent):void {
         Alert.show("IOError:" + errorEvent.text);
         trace(errorEvent.text);
          // your error-handling code here
        }
        
        private function securityErrorHandler(event:SecurityErrorEvent):void {
         trace("securityErrorHandler: " + event);
        }

       
        private function selectEvent(event:Event):void{

         btn_upload.enabled = true;
         filename.text = fileRef.name;
         fileRef.load();

        }

        private function uploadFile():void {

         createRemoteFile(fileRef.name);
         /* Alert.show(fileRef.name); */
         sendData();

        }

        private function connect():void{

         s = new Socket("ftp.mydomain.com",21);//Socket("ftp.anydomain.com",21);
         s.addEventListener(ProgressEvent.SOCKET_DATA, receiveReply);
         s.addEventListener(IOErrorEvent.IO_ERROR, showError);
         s.addEventListener(SecurityErrorEvent.SECURITY_ERROR, showSecError);
         s.addEventListener(Event.CONNECT,onSocketConnect);
         s.addEventListener(Event.CLOSE,onSocketClose);
         s.addEventListener(Event.ACTIVATE,onSocketAtivate);
       
        }
        
        private function onSocketConnect(evt:Event):void {
         //traceData("onSocketConnect–>"+evt.target.toString());
        }
       
        private function onSocketClose(evt:Event):void {
       
         //traceData("onSocketClose–>"+evt.target.toString());
        }

       

        private function onSocketAtivate(evt:Event):void {

       

         //traceData("onSocketAtivate–>"+evt.target.toString());
        }
       
        private function userName(str:String):void {

       

         sendCommand("USER "+str);
       
        }

       

        private function passWord(str:String):void {

         sendCommand("PASS "+str);

        }

        private function changeDirectory(str:String):void {

         sendCommand("CWD "+str);

        }

       

        private function sendCommand(arg:String):void {

         arg +="\n";
         s.writeUTFBytes(arg);
         s.flush();

       

        }
      ]]>
      </fx:Script>

      <mx:Panel id="up" horizontalAlign="left" width="100%" height="100%">
        <mx:Box width="100%" height="100%">
         <mx:VBox >
          <mx:Form  width="449" height="284">
           <mx:FormItem label="Selected File:">  
            <mx:Label id="filename"/>
           </mx:FormItem>
           <mx:FormItem >
            <mx:Button width="80" label="Browse" click="fileRef.browse()" />
            <mx:Button width="80" label="Upload" id="btn_upload" enabled="false"
                 click="uploadFile()" />
            <mx:Button width="80" label="Cancel" id="btn_cancel" enabled="false"
                 click="fileRef.cancel()" />
           </mx:FormItem> 
           <mx:HRule width="100%" tabEnabled="false"/>
           <mx:FormItem label="Events:">
            <mx:TextArea id="ta" width="260" height="98" />
           </mx:FormItem>
          </mx:Form> 
         </mx:VBox>
        </mx:Box>
      </mx:Panel>
      </s:WindowedApplication>

       

       

       

      App screenshot error message.jpg