Hi everyone, i just wanted to grab your attention a little and show you how easy you can write WHOIS client application in Flex. To do that you can use or Adobe Flex Builder or FlashDevelop3, doesn't mater at all in this case.
First of all you need some "Visuals", so make your mx:Application to have some TitleWindow and put there one Text Input, one TextArea and one Button.
Idea behind is quite simple, you enter domain name in your text input, press on the button and in text area you having result returned from WHOIS server.
Below you can find the ActionScript code that actually rolls whole thing.
import flash.net.Socket;
import flash.events.*;
[Bindable]
private var urlWhoisServer:String = "whois.godaddy.com";
[Bindable]
private var portWhoisServer:int = 43;
private var whois:Socket = new Socket();
private function doWhois():void
{
if (txtUrl.text != "")
{
whois.addEventListener(Event.CONNECT,socket_onConnect);
whois.addEventListener(ProgressEvent.SOCKET_DATA, socket_onData);
whois.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socket_onSecurityError);
whois.addEventListener(IOErrorEvent.IO_ERROR, socket_IoError);
whois.connect(urlWhoisServer,portWhoisServer);
}
else
{
txtResult.text = "Please provide the host name first, i.e. mydomain.com"
}
}
//Fires on connection or failure to connect
private function socket_onConnect(e:Event):void
{
if (whois.connected)
{
txtResult.text = "Connected ... Waiting ... \n\n"
var ba:ByteArray = new ByteArray();
ba.writeMultiByte(txtUrl.text + "\n", "UTF-8");
whois.writeBytes(ba);
whois.flush();
}
else
{
txtResult.text = "Unable to connect on " + urlWhoisServer;
}
}
//Fires when data available
private function socket_onData(e:Event):void
{
txtResult.text = "";
var n:int = whois.bytesAvailable;
// Loop through each available byte returned from the socket connection.
while (--n >= 0)
{
// Read next available byte.
var b:int = whois.readUnsignedByte();
txtResult.text += String.fromCharCode(b);
}
whois.close();
}
//Security error fired when security policy implementation required
//(use crossdomain.xml)
private function socket_onSecurityError(e:Event):void
{
txtResult.text = "Security Error";
}
private function socket_IoError(e:Event):void
{
txtResult.text = "Socket IO Error. Can't continue."
}
thats it, quite simple, isn't it. a bit later i will show you few ways of creating server side helpers for your flex applications.
Friday, June 22, 2007
Writing simple WHOIS client in Flex
Tags:
actionscript,
adobe,
flex
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment