User Guide

Example: Building a Telnet client 407
public function Telnet(server:String, port:int, output:TextArea)
{
serverURL = server;
portNumber = port;
ta = output;
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, dataHandler);
Security.loadPolicyFile("http://" + serverURL + "/crossdomain.xml");
try
{
msg("Trying to connect to " + serverURL + ":" + portNumber + "\n");
socket.connect(serverURL, portNumber);
}
catch (error:Error)
{
msg(error.message + "\n");
socket.close();
}
}
Writing data to a socket
To write data to a Socket connection, you call any of the write methods in the Socket class
(such as
writeBoolean(), writeByte(), writeBytes(), or writeDouble()), and then
flush the data in the output buffer using the
flush() method. In the Telnet server, data is
written to the socket connection using the
writeBytes() method which takes the byte array
as a parameter and sends it to the output buffer. The
writeBytesToSocket() method is as
follows:
public function writeBytesToSocket(ba:ByteArray):void
{
socket.writeBytes(ba);
socket.flush();
}
This method gets called by the sendCommand() method of the main application file.