2

我有一个 AS3 脚本,它连接到 FTP 服务器并检索文件/文件夹列表。当设备通过 WIFI 或 LAN 连接到互联网时脚本运行良好,但通过手机 3G 连接时无法登录

这是我的代码:

import flash.events.ProgressEvent;
import flash.events.Event;
import flash.net.Socket;
import flash.events.IOErrorEvent;
import flash.errors.IOError;
//
var ftp_host:String="ftp.server.com";
var ftp_port:Number=21//or your ftp port;
var ftp_username:String="username";
var ftp_password:String="password";
var ftp_path:String="/";
var s,r;
var listOfFiles:Array;
//


readFromFTP()

function readFromFTP(e=null){
    s = new Socket(ftp_host,ftp_port);
    r = new Socket();
    listOfFiles=new Array();
    s.addEventListener(IOErrorEvent.IO_ERROR,onIOERR);
    s.addEventListener(ProgressEvent.SOCKET_DATA, onReturnData);
    s.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecERR);
    r.addEventListener(ProgressEvent.SOCKET_DATA, onServData);
    r.addEventListener(Event.CONNECT, onPasvConn);
    r.addEventListener(IOErrorEvent.IO_ERROR,onIOERR);
    r.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecERR);

    loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, handleGlobalErrors);
}


function onReturnData(evt:ProgressEvent)
{
    var d = s.readUTFBytes(s.bytesAvailable);
    trace(d);
    text_txt.appendText(d);
    if(d.indexOf("220 ")>-1){
       text_txt.appendText("Username sent\n");
       s.writeUTFBytes("USER "+ftp_username+"\n");
       s.flush()
    }
    if(d.indexOf("331 ")>-1){
        text_txt.appendText("Password sent\n");
       s.writeUTFBytes("PASS "+ftp_password+"\n");
       s.flush()
    }
    if (d.indexOf("230") > -1)
    {
        text_txt.appendText("PASV command sent\n");
        s.writeUTFBytes("PASV \n");
        s.flush();
    }
    var a = d.indexOf('227');
    if (a > -1)
    {
        var st = d.indexOf("(",a);
        var en = d.indexOf(")",a);
        var str;
        str = d.substring(st + 1,en);
        var a2 = str.split(",");
        var p1 = a2.pop();
        var p2 = a2.pop();
        var ip:String = a2.join(".");
        var port:int=(p2*256)+(p1*1);      
        r.connect(ip, port);
    }
    if(d.indexOf("226 ")>-1){
        text_txt.appendText("QUIT command sent\n");
        s.writeUTFBytes("QUIT \n");
        s.flush();
    }
    if(d.indexOf("221 ")>-1){
    }
}
function onPasvConn(evt:Event):void
{
    //trace("CONNECTED TO DATA PORT");
    text_txt.appendText("CONNECTED TO DATA PORT\n");
    s.writeUTFBytes("NLST "+ftp_path+"\n");
    s.flush();
}
function onServData(evt:ProgressEvent):void
{
    var d = r.readUTFBytes(r.bytesAvailable);
    trace(d);
    text_txt.appendText(d);
}
function onIOERR(evt:IOErrorEvent)
{
    trace(evt.errorID+":"+evt.text);
    text_txt.appendText(evt.errorID+":"+evt.text);
}

function onSecERR(evt){
    text_txt.appendText("Security error\n");
}

function handleGlobalErrors(evt){
    text_txt.appendText("Unhandeled Error\n");
}
4

2 回答 2

2

Finaly I fixed the bug.

For some reason the server didn't respond to command with (\n) linebreaks.

I changed it to (\r\n) and it's working fine now

I hope this helps someone

于 2012-08-25T07:14:41.997 回答
0

很多 3G 运营商会阻塞某些端口,这会导致它出现故障。

可以肯定的是,AS3 本身在 3G 上的行为并没有什么不同。

于 2012-07-31T11:42:14.860 回答