A Port Scanner using ActionScript 3 Socket in Macromedia Flash Player 8.5 alpha

I just wrote a simple port-scanner in AS3(ActionScript 3) using flash.net.Socket class in Macromedia Flash Player 8.5 alpha. I have used Macromedia FlexBuilder 2 alpha to create the user-interface but most of the code is in AS3.
I have used my own logic for port-scanning, I am not aware how it is done in actual port-scanners. Since it is a simple example, which means I have not taken care of performance, error-handling, user-input-validation etc.
I was looking for a port-scanner on internet and then this idea popped up. It works for me. I can check which all ports on my system are open. I can also check for open ports on a remote-host as well.
Here is code:


<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" width="600" height="600">
<mx:Script>
<![CDATA[
import flash.net.*;
import flash.events.*;
private var socket:Socket;
private var currentPort:uint;
private var toPort:uint;
private function createSocket():void
{
socket = new Socket();
socket.addEventListener("connect", socket_connect);
socket.addEventListener("ioError", socket_ioError);
}
private function deleteSocket():void
{
socket.removeEventListener("connect",socket_connect);
socket.removeEventListener("ioError", socket_ioError);
socket.close ();
}
private function startScanning():void
{
createSocket();
currentPort = uint(fromPort_ti.text);
toPort = uint(toPort_ti.text);
scanPort(currentPort);
}
private function scanPort(port:uint):void
{
if(port <= toPort)
{
status.text = "Scanning port:" + port + "\n";
socket.connect(host.text, port);
enableControls(false);
}
else
{
enableControls(true);
deleteSocket();
status.text = "Scanning complete..";
}
}
private function socket_connect(event:Event):void
{
openPorts_ta.text += currentPort + "\n";
currentPort++;
socket.close();
scanPort(currentPort);
}
private function socket_ioError(event:IOErrorEvent):void
{
/*
Not sure, when there is ioError I am not able to reuse same socket object.
So deleting current socket object and recreating another socket object...
*/
deleteSocket();
createSocket();
scanPort(++currentPort);
}
private function enableControls(bEnable:Boolean):void
{
host.enabled = bEnable;
toPort_ti.enabled  = bEnable;
fromPort_ti.enabled = bEnable;
scanButton.enabled = bEnable;
}
]]>
</mx:Script>
<mx:Form>
<mx:FormItem label="Host:"><mx:TextInput id="host" text="127.0.0.1"/></mx:FormItem>
<mx:FormItem label="Start port:"><mx:TextInput id="fromPort_ti" text="1"/></mx:FormItem>
<mx:FormItem label="To port:"><mx:TextInput id="toPort_ti" text="65535"/></mx:FormItem>
<mx:FormItem><mx:Button id="scanButton" label="Start Scannning" click="startScanning()"/></mx:FormItem>
</mx:Form>
<mx:VBox>
<mx:Label text="Open Ports:"/>
<mx:TextArea id="openPorts_ta" width="400" height="250"/>
<mx:Label id="status"/>
</mx:VBox>
</mx:Application>


Download file


Update: This post was written when Adobe Flash Player 9 was not released and was known as Flash Player 8.5. I have updated the code for Adobe Flash Player 9.