[AS3] – FTP CLIENT – PART I

April 18, 2011 at 2:31 AM (Actionscript) (, , )

Sharing here my attempt to create a ftp client in Adobe AIR(you can also try it in flash player but you may face security issue, you have to provide policy file) …before getting into the bigger picture, in this part 1 ill show you how to connect to a ftp server, login and retrieve directory listing of a folder in the server….
In next part lets see how to upload and download files from ftp server…


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.yourhost.com";
var ftp_port:Number=21//or your ftp port;
var ftp_username:String="username";
var ftp_password:String="password";
//
//Socket instance which will be used to connect to ftp server
var s = new Socket(ftp_host,ftp_port);
s.addEventListener(IOErrorEvent.IO_ERROR,onIOERR);
s.addEventListener(ProgressEvent.SOCKET_DATA, onReturnData);
s.addEventListener(Event.CONNECT, onConnectHandler);
//
//Socket instance which will be used to connect to receive data sent by ftp server
var r:Socket = new Socket();
r.addEventListener(ProgressEvent.SOCKET_DATA, onServData);
r.addEventListener(Event.CONNECT, onPasvConn);
r.addEventListener(IOErrorEvent.IO_ERROR,onIOERR);
//For every command the client sends to ftp server the server returns message with return codes
function onReturnData(evt:ProgressEvent)
{
	var d = s.readUTFBytes(s.bytesAvailable);
	trace(d);
	//check here for complete list of return codes and their meaning
	//- http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes
	
	// the return message will have a 3 digit return code followed by a space and related message
	// if the 3 digit return code is followed by "-" the it will be a multiline message
	//-wait until the line with 3 digit code followed by space is delivered
	if(d.indexOf("220 ")>-1){
	   //connected to ftp server send user name to server
	   s.writeUTFBytes("USER "+ftp_username+"\n");
	   s.flush()
	}
	if(d.indexOf("331 ")>-1){
	    //Username accepted now send password to server
	   s.writeUTFBytes("PASS "+ftp_password+"\n");
	   s.flush()
	}
	if (d.indexOf("230") > -1 && d.indexOf("OK.") > -1)
	{
		//Password accepted - lets enter passive mode and retrieve a list of files from a directory
		//first enter passive mode
		s.writeUTFBytes("PASV \n");
		s.flush();
	}
	var a = d.indexOf('227');
	if (a > -1)
	{
		//Entering passive mode message will be returned along with it details of ip and port address will be returned
		//-we have to connect to that address to receive the data
		//format of the message will be: 227 Entering Passive Mode (209,190,85,253,148,206)
		//the data inside brackets is the ip and port address, first four numbers represent IP and last 2 PORT
		//the port value have to be calculated by multiplying the 5th number with 256 and adding the 6th number to it
		//here in this example IP is 209.190.85.253 , PORT is (148*256)+206 = 38094
		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){
		//Data Transfer completely lets disconnect from server
		s.writeUTFBytes("QUIT \n");
		s.flush();
	}
	if(d.indexOf("221 ")>-1){
		//LOGGED OUT from server 
	}
}
function onConnectHandler(evt:Event)
{
	trace("CONNECTED TO FTP SERVER");
	//Client has connected to ftp server
	//you can also send multiple commands at same time like below or send step by step based on return code
	//-!
	//s.writeUTFBytes("USER username\n");
	//s.writeUTFBytes("PASS password\n"); 
	//s.flush();
}//
function onPasvConn(evt:Event):void
{
	trace("CONNECTED TO DATA PORT");
	//Now send LIST command to retrieve directory listings
	s.writeUTFBytes("LIST /path/to/yourfolder\n");
	s.flush();
}
function onServData(evt:ProgressEvent):void
{
	//DATA RECEIVED FROM SERVER THRO DATA PORT
	var d = r.readUTFBytes(r.bytesAvailable);
	trace(d);
}
function onIOERR(evt:IOErrorEvent)
{
	trace(evt.errorID+":"+evt.text);
}

Permalink 10 Comments