13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Sockets<br />

private var serverURL:String;<br />

private var portNumber:int;<br />

private var socket:Socket;<br />

private var ta:TextArea;<br />

private var state:int = 0;<br />

The first variable, serverURL, contains the user-specified server address to connect to.<br />

The second variable, portNumber, is the port number that the Telnet server is curr<strong>en</strong>tly running on. By default, the<br />

Telnet service runs on port 23.<br />

The third variable, socket, is a Socket instance that attempts to connect to the server defined by the serverURL and<br />

portNumber variables.<br />

The fourth variable, ta, is a refer<strong>en</strong>ce to a TextArea compon<strong>en</strong>t instance on the Stage. This compon<strong>en</strong>t is used to<br />

display responses from the remote Telnet server, or any possible error messages.<br />

The final variable, state, is a numeric value that is used to determine which options your Telnet cli<strong>en</strong>t supports.<br />

As you saw before, the Telnet class's constructor function is called by the connect() method in the main application file.<br />

The Telnet constructor takes three parameters: server, port, and output. The server and port parameters specify<br />

the server name and port number where the Telnet server is running. The final parameter, output, is a refer<strong>en</strong>ce to a<br />

TextArea compon<strong>en</strong>t instance on the Stage where server output is displayed to users.<br />

public function Telnet(server:String, port:int, output:TextArea)<br />

{<br />

serverURL = server;<br />

portNumber = port;<br />

ta = output;<br />

socket = new Socket();<br />

socket.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.CONNECT, connectHandler);<br />

socket.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.CLOSE, closeHandler);<br />

socket.addEv<strong>en</strong>tList<strong>en</strong>er(ErrorEv<strong>en</strong>t.ERROR, errorHandler);<br />

socket.addEv<strong>en</strong>tList<strong>en</strong>er(IOErrorEv<strong>en</strong>t.IO_ERROR, ioErrorHandler);<br />

socket.addEv<strong>en</strong>tList<strong>en</strong>er(ProgressEv<strong>en</strong>t.SOCKET_DATA, dataHandler);<br />

Security.loadPolicyFile("http://" + serverURL + "/crossdomain.xml");<br />

try<br />

{<br />

msg("Trying to connect to " + serverURL + ":" + portNumber + "\n");<br />

socket.connect(serverURL, portNumber);<br />

}<br />

catch (error:Error)<br />

{<br />

msg(error.message + "\n");<br />

socket.close();<br />

}<br />

}<br />

Writing data to a socket<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

To write data to a socket connection, you call any of the write methods in the Socket class. These write methods include<br />

writeBoolean(), writeByte(), writeBytes(), writeDouble(), and others. Th<strong>en</strong>, flush the data in the output<br />

buffer using the flush() method. In the Telnet server, data is writt<strong>en</strong> to the socket connection using the<br />

writeBytes() method which takes the byte array as a parameter and s<strong>en</strong>ds it to the output buffer. The<br />

writeBytesToSocket() method is as follows:<br />

Last updated 6/6/2012<br />

800

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!