package { import flash.events.*; import flash.errors.*; import flash.net.Socket; import flash.system.Security; import flash.utils.ByteArray; import flash.utils.setTimeout; import flash.utils.* import flash.display.Sprite; public class Telnet extends Sprite{ private const CR:int = 13; // Carriage Return (CR) private const WILL:int = 0xFB; // 251 - WILL (option code) private const WONT:int = 0xFC; // 252 - WON'T (option code) private const DO:int = 0xFD; // 253 - DO (option code) private const DONT:int = 0xFE; // 254 - DON'T (option code) private const IAC:int = 0xFF; // 255 - Interpret as Command (IAC) private var serverURL:String; private var portNumber:int; private var socket:Socket; private var state:int = 0; private static var clients:int = 0; public function Telnet() { //Telnet(server:String, port:int) //serverURL = server; //portNumber = port; // Create a new Socket object and assign event listeners. socket = new Socket(); socket.addEventListener(Event.CONNECT, connectHandler); socket.addEventListener(Event.CLOSE, closeHandler); socket.addEventListener(ErrorEvent.ERROR, errorHandler); socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); socket.addEventListener(ProgressEvent.SOCKET_DATA, progressHandler); // Load policy file from remote server. //Security.loadPolicyFile("http://" + serverURL + "/crossdomain.xml"); //Security.loadPolicyFile("/home/elyasov/FITTEST/ActionScript/Server/crossdomain.xml"); // Attempt to connect to remote socket server. trace("tset"); socket.connect("localhost", 10515); socket.writeUTF("test" + "\n"); socket.flush(); socket.close(); } /** * This method is called if the socket encounters an ioError event. */ public function ioErrorHandler(event:IOErrorEvent):void { msg("Unable to connect: socket error.\n"); } // public function writeBytesToSocket(ba:ByteArray):void { // socket.writeBytes(ba); // socket.flush(); // } public function writeBytesToSocket(str:ByteArray):void { var timer:Timer = new Timer(1000,10); timer.addEventListener(TimerEvent.TIMER, timerHandler); timer.start(); function timerHandler(event:TimerEvent):void { if (socket.connected) { trace("Socket is connected"); socket.writeBytes(str); socket.flush(); timer.stop(); } else (trace("timer continuing")); } } public function closeTelnet():void { var timer:Timer = new Timer(1000,10); timer.addEventListener(TimerEvent.TIMER, timerHandler); timer.start(); function timerHandler(event:TimerEvent):void { if (socket.bytesAvailable == 0) { trace("Socket can be closed"); socket.close(); clients--; trace(clients); timer.stop(); } else { (trace("timer continuing")); } } } private function connectHandler(event:Event):void { //clients++; } 1 /** * This method is called when the socket connection is closed by * the server. */ private function closeHandler(event:Event):void { //clients--; } /** * This method is called if the socket throws an error. */ private function errorHandler(event:ErrorEvent):void { msg(event.text + "\n"); } private function progressHandler1(event:ProgressEvent):void { var n:int = socket.bytesAvailable; // Loop through each available byte returned from the socket connection. while (--n >= 0) { // Read next available byte. var b:int = socket.readUnsignedByte(); switch (state) { case 0: // If the current byte is the "Interpret as Command" code, set the state to 1. if (b == IAC) { state = 1; // Else, if the byte is not a carriage return, display the character using the msg() method. } else if (b != CR) { msg(String.fromCharCode(b)); } break; case 1: // If the current byte is the "DO" code, set the state to 2. if (b == DO) { state = 2; } else { state = 0; } break; // Blindly reject the option. case 2: /* Write the "Interpret as Command" code, "WONT" code, and current byte to the socket and send the contents to the server by calling the flush() method. */ socket.writeByte(IAC); socket.writeByte(WONT); socket.writeByte(b); socket.flush(); state = 0; break; } } } private function progressHandler(event:ProgressEvent):void { trace(event.cancelable) } private function msg(value:String):void { trace (value); } } }