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.
import mx.central.Shell;
import mx.central.Central;
import mx.utils.Delegate;
import mx.controls.Button;
class TestCFT implements Application{
private static var __minW:Number = 500;
private static var __minH:Number = 500;
private static var __FCS_PATH:String = "rtmp://localhost/filetransfer/";
private var __shell:Shell;
private var __mc:MovieClip;
private var __nc:NetConnection;
private var __outstream:NetStream;
private var __instream:NetStream;
public function TestCFT(oMc:MovieClip){
__mc = oMc;
Central.initApplication(__mc, this);
}
public function init():Void{
__nc = new NetConnection();
__nc.connect(TestCFT.__FCS_PATH);
__nc.onStatus = Delegate.create(this, onStatus);
draw();
}
public function draw():Void{
var depth:Number = 0;
var sendBtn:Button = Button(__mc.attachMovie( "Button", "send_btn", depth++ ));
var img:MovieClip = __mc.createEmptyMovieClip("image_mc", depth++);
sendBtn.label = "send file";
sendBtn.move(10, 10);
sendBtn.clickHandler = Delegate.create(this, sendFile);
img._x = 10;
img._y = sendBtn.y + sendBtn.height + 10;
}
public function renderFile(oFile:Object):Void{
var f:FileReference = new FileReference();
f.create(oFile.name);
f.writeBytes(oFile.bytes);
f.close();
__mc.image_mc.loadMovie(oFile.name);
}
public function sendFile():Void{
var f = new FileReference();
var fTransfer:Object = {};
if (f.browse()) {
fTransfer.name = f.name;
fTransfer.bytes = f.readBytes(f.size);
f.close();
}
__outstream.send("renderFile", fTransfer);
}
public function onStatus(oInfo:Object):Void{
if(oInfo.code == "NetConnection.Connect.Success"){
__outstream = new NetStream(__nc);
__instream = new NetStream(__nc);
__instream["renderFile"] = Delegate.create(this, renderFile);
__outstream.publish("filetransfer");
__instream.play("filetransfer", -1);
}
}
public function getMinimumSize(Void):Object{
return {width:__minW, height:__minH};
}
public function onActivate(oShell:Shell, iAppID:Number, iShellID:Number, iBaseTabIndex:Number, oInitData:Object):Void{
__shell = oShell;
init();
}
public function onResize(Void):Void{ }
public function onDeactivate(Void):Void{}
public function onNetworkChange(bIsConnected:Boolean):Void{}
public function onNoticeEvent(oEvent:Object, oData:Object, oInitData:Object):Void{}
public function onPaymentResult(bResult:Boolean, oTransactionID:Object):Void{}
public function onSelectedItem(aData:Array):Void{}
public function onUninstall(Void):Void{}
public function showPreferences(Void):Void{}
}
October 28th, 2004 at 8:18 am
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?
October 28th, 2004 at 9:27 am
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.
October 28th, 2004 at 1:40 pm
v cool!
October 28th, 2004 at 2:26 pm
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?
November 8th, 2005 at 1:36 am
Great Idea.
I have question about why using central? My guest is about FileReference(). This is also available on flash 8
February 20th, 2006 at 8:19 pm
[...] About 3-4 month ago based on Kevin Bunch idea about file transfers with central and flashcomm using bytecode. I tried to created the same thing but not with central. Because since on Flash 8 has the FileReference class we already possible to open dialogBox to select file to upload. Combined with PHP to get bytecode of files the idea is working but just with small files (we talk about bytecode data sent to FMS. Then I need to know how we can split up bytcode and sent it continously. My friend, Mario told me to use java socket. Well good idea I said, but I leave it since I have 2 big projects and will back for it. Until this morning I have read about file sharing using FMS from Flash Media Server Fun, they called it Flash P2P. They using FMS + JAVA Servlet to make it work. This great. So don’t miss this esspecially they also provide the source [ZIP] [RAR]. Comments » [...]
May 14th, 2006 at 8:51 pm
[...] About 3-4 month ago based on Kevin Bunch idea about file transfers with central and flashcomm using bytecode. I tried to created the same thing but not with central. Because since on Flash 8 has the FileReference class we already possible to open dialogBox to select file to upload. Combined with PHP to get bytecode of files the idea is working but just with small files (we talk about bytecode data sent to FMS. Then I need to know how we can split up bytcode and sent it continously. My friend, Mario told me to use java socket. Well good idea I said, but I leave it since I have 2 big projects and will back for it. Until this morning I have read about file sharing using FMS from Flash Media Server Fun, they called it Flash P2P. They using FMS + JAVA Servlet to make it work. This great. So don’t miss this esspecially they also provide the source [ZIP] [RAR]. [...]
July 26th, 2007 at 6:15 am
Can we eliminate FlasComm completely here?
Denis
http://abava.blogspot.com