A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: as3 read/write over ftp socket to NAS server

  1. #1
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404

    as3 read/write over ftp socket to NAS server

    simple script for read/write to NAS through ftp with last 2 functions to read and write
    PHP Code:
    var socketData:ByteArray;
    var 
    user="anonymous";
    var 
    pass="";
    var 
    ip="10.0.0.208";//ethernet=10.0.0.208//wifi=192.168.169.1
    var port=21
    var task="";

    var 
    socket_ftp = new Socket();
    socket_ftp.addEventListener(ProgressEvent.SOCKET_DATAonFtpSocketData);
    socket_ftp.addEventListener(Event.CONNECTconnected);
    socket_ftp.addEventListener(IOErrorEvent.IO_ERROR,err);
    var 
    socket_passive:Socket = new Socket();
    socket_passive.addEventListener(ProgressEvent.SOCKET_DATAonPassiveSocketData);
    socket_passive.addEventListener(Event.CONNECTonPassiveConnect);
    socket_passive.addEventListener(IOErrorEvent.IO_ERROR,err);
    var 
    cnt=0
    function connected(e:*){
    }
    function 
    onPassiveConnect(e:*){
    if (
    task=="create_file"){
    socket_ftp.writeUTFBytes("STOR "+"/share/sda1/11111.txt"+"\n");
    cnt++
    trace("sent")
    }
    if (
    task=="request_file"){
    socket_ftp.writeUTFBytes("RETR "+"/share/sda1/11111.txt"+"\n");
    }
    if (
    task=="getDirList"){
    socket_ftp.writeUTFBytes("LIST "+"/share/sda1"+"\n");
    }
    socket_ftp.flush();
    }

    function 
    onPassiveSocketData(e:*){
    socketData=new ByteArray();
    socket_passive.readBytes(socketData,0,socket_passive.bytesAvailable);
    socket_passive.close();
    trace("file contents: " +socketData)
    }
    var 
    ftp_array;
    var 
    ftp_code;
    function 
    onFtpSocketData(e:*){
    var 
    parsedString=socket_ftp.readUTFBytes(socket_ftp.bytesAvailable);

    if(
    parsedString.indexOf("550 ")>-1){
    socket_ftp.close();
    }
    if (
    task=="request_file"){

    }
    if(
    task=="getDirList"){
    }
    ftp_code parsedString.indexOf("227");

    if (
    ftp_code > -1){
    ftp_array parsedString.substring(parsedString.indexOf("(",parsedString.indexOf("227"))+1,parsedString.indexOf(")",parsedString.indexOf("227"))).split(",");
    var 
    passive_port:int=(ftp_array[4]*256)+(int(ftp_array[5]));
    socket_passive.connect(ip,passive_port);
    }

    if (
    parsedString.indexOf("226 ") > -1){
    socket_ftp.close();
    }
    if(
    task=="create_file"&&parsedString.indexOf("150 ") > -1){
    socket_passive.writeUTFBytes("file contents here");
    socket_passive.flush();
    socket_ftp.close();
    socket_passive.close();
    }
    }
    addEventListener(MouseEvent.MOUSE_DOWN,md);
    function 
    md(e:*){
    if(
    e.target.name=="create_file"&&socket_ftp.connected==false){
    task "create_file";
    socket_ftp.connect(ip,port);
    socket_ftp.writeUTFBytes("USER "+user+"\n"+"PASS "+pass+"\n"+"TYPE I\n"+"PASV \n");
    socket_ftp.flush();
    }
    if(
    e.target.name=="request_file"&&socket_ftp.connected==false){
    task "request_file";
    socket_ftp.connect(ip,port);
    socket_ftp.writeUTFBytes("USER "+user+"\n"+"PASS "+pass+"\n"+"TYPE I\n"+"PASV \n");
    socket_ftp.flush();
    }
    }
    addEventListener(Event.ENTER_FRAME,loop)
    function 
    loop(e:*){
    //trace(socket_ftp.connected + " " + socket_passive.connected)
    }
    function 
    err(e:*){
    }

    function 
    request_file(){
    if(
    socket_ftp.connected==false){
    task "request_file";
    socket_ftp.connect(ip,port);
    socket_ftp.writeUTFBytes("USER "+user+"\n"+"PASS "+pass+"\n"+"TYPE I\n"+"PASV \n");
    socket_ftp.flush();
    }
    }
    function 
    create_file(){
    if(
    socket_ftp.connected==false){
    task "create_file";
    socket_ftp.connect(ip,port);
    socket_ftp.writeUTFBytes("USER "+user+"\n"+"PASS "+pass+"\n"+"TYPE I\n"+"PASV \n");
    socket_ftp.flush();
    }
    }
    create_file()
    //request_file() 
    Last edited by AS3.0; 12-11-2018 at 01:38 AM.

  2. #2

  3. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    1
    Was this code written by you, Alloy Bacon?

  4. #4
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Yes. Its free. And I wouldnt be able to do it without the developer guides because thats like knowing your way through someones house without asking them. I didnt invent ftp i just released a simpler version for myself to use on this NAS device I have so multiple as3 servers on various local computers can read and write files to store as a database. Its like 1 server goes down and the other one still has all the same data for the clients connecting.

    For sftp which is a whole different modern secure version I have as3 libraries like Blowfish symmetric-key block cipher to handle the data but only thing I stopped writing it because I cant tell where the first 14 bytes are coming from when recording the data with a tcp packet capture, sftp is good to communicate with modern webservers sftp like lamp to transfer from as3 to lamp etc. I dont think i will finish sftp because its for the NAS which is modern device that still uses plain ftp
    Last edited by AS3.0; 11-21-2019 at 04:22 PM.

  5. #5
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    I have written an as3 ftp like above that can do more like handle a queue of multiple files you need made with unique data per file. I tested it to create 50k different files and they were all in the queue and eventually made pretty fast. ill sell the ftp version thats multithreaded for like $5 paypal if someone wants

  6. #6
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    $5 for my multithreaded as3 ftp read/write script and ill buy something off you later on of around the same value that you made and want to sell.
    Last edited by AS3.0; 11-22-2019 at 08:03 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center