File Transfer with Central and Flashcom

I had a theory I wanted to test this morning : Can I transfer files from point 2 point using Central and some sort of proxy. 5 minutes of thought, 5 minutes of code, and the answer was yes. Basically I just read in a binary file, sent the binary data across the wire, wrote the binary on receive, and opened it up. Below is the echo code. NOTE: the flashcom app does nothing but accepts the connection.

Actionscript:
  1. import mx.central.Application;
  2. import mx.central.Shell;
  3. import mx.central.Central;
  4. import mx.utils.Delegate;
  5. import mx.controls.Button;
  6.  
  7. class TestCFT implements Application{
  8.  
  9.      private static var __minW:Number = 500;
  10.      private static var __minH:Number = 500;
  11.      private static var __FCS_PATH:String = "rtmp://localhost/filetransfer/";
  12.  
  13.      private var __shell:Shell;
  14.      private var __mc:MovieClip;
  15.      private var __nc:NetConnection;
  16.      private var __outstream:NetStream;
  17.      private var __instream:NetStream;
  18.  
  19.      public function TestCFT(oMc:MovieClip){
  20.  
  21.           __mc = oMc;
  22.           Central.initApplication(__mc, this);
  23.      }
  24.  
  25.      public function init():Void{
  26.  
  27.           __nc = new NetConnection();
  28.           __nc.connect(TestCFT.__FCS_PATH);
  29.           __nc.onStatus = Delegate.create(this, onStatus);
  30.  
  31.           draw();
  32.      }
  33.  
  34.      public function draw():Void{
  35.  
  36.           var depth:Number = 0;
  37.           var sendBtn:Button = Button(__mc.attachMovie( "Button", "send_btn", depth++ ));
  38.           var img:MovieClip = __mc.createEmptyMovieClip("image_mc", depth++);
  39.  
  40.           sendBtn.label = "send file";
  41.           sendBtn.move(10, 10);
  42.           sendBtn.clickHandler = Delegate.create(this, sendFile);
  43.           img._x = 10;
  44.           img._y = sendBtn.y + sendBtn.height + 10;
  45.      }
  46.  
  47.      public function renderFile(oFile:Object):Void{
  48.  
  49.           var f:FileReference = new FileReference();
  50.  
  51.           f.create(oFile.name);
  52.           f.writeBytes(oFile.bytes);
  53.           f.close();
  54.  
  55.           __mc.image_mc.loadMovie(oFile.name);
  56.      }
  57.  
  58.      public function sendFile():Void{
  59.  
  60.           var f = new FileReference();
  61.           var fTransfer:Object = {};
  62.  
  63.           if (f.browse()) {
  64.                fTransfer.name = f.name;
  65.                fTransfer.bytes = f.readBytes(f.size);
  66.                f.close();
  67.           }
  68.  
  69.           __outstream.send("renderFile", fTransfer);
  70.      }
  71.  
  72.      public function onStatus(oInfo:Object):Void{
  73.  
  74.           if(oInfo.code == "NetConnection.Connect.Success"){
  75.                __outstream = new NetStream(__nc);
  76.                __instream = new NetStream(__nc);
  77.                __instream["renderFile"] = Delegate.create(this, renderFile);
  78.                __outstream.publish("filetransfer");
  79.                __instream.play("filetransfer", -1);
  80.           }
  81.      }
  82.  
  83.      public function getMinimumSize(Void):Object{
  84.  
  85.           return {width:__minW, height:__minH};
  86.      }
  87.  
  88.      public function onActivate(oShell:Shell, iAppID:Number, iShellID:Number, iBaseTabIndex:Number, oInitData:Object):Void{
  89.  
  90.           __shell = oShell;
  91.  
  92.           init();
  93.      }
  94.  
  95.      public function onResize(Void):Void{ }
  96.      public function onDeactivate(Void):Void{}
  97.      public function onNetworkChange(bIsConnected:Boolean):Void{}
  98.      public function onNoticeEvent(oEvent:Object, oData:Object, oInitData:Object):Void{}
  99.      public function onPaymentResult(bResult:Boolean, oTransactionID:Object):Void{}
  100.      public function onSelectedItem(aData:Array):Void{}
  101.      public function onUninstall(Void):Void{}
  102.      public function showPreferences(Void):Void{}
  103. }

  1. #1 by Byron Saltysiak - October 28th, 2004 at 08:18

    Pretty sweet idea man. So when is CentralTorrent coming out ;)

    Seriously though I think you’ll have problems with large files using this method since you’re reading the entire file into memory (or trying to). Have you found any filesize limits or practical concerns?

  2. #2 by Kenny Bunch - October 28th, 2004 at 09:27

    Byron,

    Yes, this technique has it’s issues, and may not be something that you would even use. I think the concept is the important piece. I just wanted to show a possibility, others will probably figure out the best way to do it.

  3. #3 by daz - October 28th, 2004 at 13:40

    v cool!

  4. #4 by Byron Saltysiak - October 28th, 2004 at 14:26

    Yeah definitely cool – I didn’t really get to finish my thought. Actually a couple questions mostly due to my lack of knowledge about Flashcomm. Doesn’t Flashcomm require a server? How does one user connect directly to another?

    Also, I was thinking about traditional sockets and thinking that large files work sort of similar to streaming right… the sender fills a send buffer one block at a time and the receiver reads from the receive buffer until eventually the entire datastream is complete and the socket is closed. So the question is: Could you change this implementation to use Flashcomm’s file streaming?

  5. #5 by Janu - November 8th, 2005 at 01:36

    Great Idea.
    I have question about why using central? My guest is about FileReference(). This is also available on flash 8

  6. #6 by Denis - July 26th, 2007 at 06:15

    Can we eliminate FlasComm completely here?

    Denis
    http://abava.blogspot.com

(will not be published)
Submit Comment
Subscribe to comments feed