dcsimg
A Flash Developer Resource Site

Results 1 to 2 of 2

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

  1. #1
    Member Alloy Bacon's Avatar
    Join Date
    Apr 2011
    Posts
    1,017

    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 Alloy Bacon; 12-11-2018 at 01:38 AM.

  2. #2

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