0 Replies Latest reply on Oct 2, 2010 8:16 AM by jsquared90

    Sending files between AIR applications

    jsquared90

      Hi,

       

      I am working on an application that shares files between 2 remote AIR applications. I've successfully set up a process using ServerSocket on the receive end and Socket on the transmit end, reading the selected file's data, and transmitting the data vie bytes (ByteArray) through the socket connection.

       

      I noticed that the data becomes parsed into packets of 65536 bytes (as expected) when transmitted. What I am experiencing is incredibly long (sometimes over 500ms) periods of time between receipts of these packets. I'm not sure if it's innate to the process or aspects of Flash (ie: single threading), or if I am doing something wrong. I just figured data would transfer much faster. Below is my code.... any help/suggestions would be very appreciated.

       

      I'm thinking of possibly opening multiple sockets with pre-parsed arrays of the data, and then re-building on the Rx end when all packets have been consumed. I'm not sure if that will speed up the process?

       

      Many thanks,

       

      JJ

       

      SENDING APP :

       

      var file:File = new File();

      var fileData:ByteArray;

      var fileTransferSocketMon:SocketMonitor;

      var fileTransferSocket:Socket;

       

      file.addEventListener(Event.SELECT , fileSelectHandler);

      file.browseForOpen("Select a file to transmit");

       

      private function fileSelectHandler(event:Event):void

      {

           var file:* = event.target;

           var stream:FileStream = new FileStream();

           stream.open(file, FileMode.READ);

           fileData = new ByteArray();

           stream.readBytes(fileData);

           fileTransferSocketMon = new SocketMonitor("192.168.0.1" , 10006);

           fileTransferSocketMon.addEventListener(StatusEvent.STATUS , fileSockMonStatus);

           fileTransferSocketMon.pollInterval = 1000;

           fileTransferSocketMon.start();

      }

       

      private function fileSockMonStatus(event:StatusEvent):void

      {

           if (fileTransferSocketMon.available)

           {

                fileTransferSocket = new Socket(fileSendAddress.text , int(fileSendPort.text));

                fileTransferSocket.addEventListener(Event.CONNECT , fileTransferSocketConnectHandler);

                fileTransferSocket.addEventListener(Event.CLOSE , fileTransferSocketCloseHandler);

                fileTransferSocket.connect(fileSendAddress.text , int(fileSendPort.text));

                fileTransferSocketMon.stop();

                fileTransferSocketMon = null;

           }

      }

       

       

      private function fileTransferSocketConnectHandler(event:Event):void

      {

           event.target.writeBytes(fileData);

      }

       

       

      private function fileTransferSocketCloseHandler(event:Event):void

      {

           fileTransferSocket.removeEventListener(Event.CONNECT , fileTransferSocketConnectHandler);

           fileTransferSocket.removeEventListener(Event.CLOSE , fileTransferSocketCloseHandler);

           fileTransferSocket.close();

           fileTransferSocket = null;

      }

       

       

       

      RECEIVING APP :

       

      var fileTransferData:ByteArray = new ByteArray();

      var fileTransferSocket:ServSocket = new ServerSocket();

      fileTransferSocket.bind(10006 , "192.168.0.2");

      fileTransferSocket.addEventListener(ServerSocketConnectEvent.CONNECT , fileTransferSocketConnectHandler);

      fileTransferSocket.listen();

       

      private function fileTransferSocketConnectHandler(event:ServerSocketConnectEvent):void

      {

           var clientSocket:Socket = event.socket;

           clientSocket.addEventListener(ProgressEvent.SOCKET_DATA , fileTransferDataHandler);

      }

       

      private function fileTransferDataHandler(event:ProgressEvent):void

      {

           var clientSocket:* = event.target;

           clientSocket.readBytes(fileTransferData , fileTransferData.length , event.target.bytesAvailable);

       

           // Temporary hack job. Will eventually convey target file size to Rx APP in advance, along with various metadata for file (save name & location, etc...)

           if (event.target.bytesAvailable < 65536)

           {

                trace("writing file");

                var file:File = new File("/testfile.mov");

                var fileStream:FileStream = new FileStream();

                fileStream.open(file , FileMode.WRITE);

                fileStream.writeBytes(fileTransferData);

                fileStream.close();

                clientSocket.removeEventListener(ProgressEvent.SOCKET_DATA , fileTransferDataHandler);

                fileTransferSocket.removeEventListener(ServerSocketConnectEvent.CONNECT , fileTransferSocketConnectHandler);

                fileTransferSocket.close();

                fileTransferSocket = null;

           }

      }