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 />

File Description<br />

TelnetSocket.fla<br />

or<br />

TelnetSocket.mxml<br />

Telnet socket application overview<br />

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

The main TelnetSocket.mxml file is responsible for creating the user interface (UI) for the <strong>en</strong>tire application.<br />

In addition to the UI, this file also defines two methods, login() and s<strong>en</strong>dCommand(), to connect the user to the<br />

specified server.<br />

The following code lists the ActionScript in the main application file:<br />

import com.example.programmingas3.socket.Telnet;<br />

private var telnetCli<strong>en</strong>t:Telnet;<br />

private function connect():void<br />

{<br />

telnetCli<strong>en</strong>t = new Telnet(serverName.text, int(portNumber.text), output);<br />

console.title = "Connecting to " + serverName.text + ":" + portNumber.text;<br />

console.<strong>en</strong>abled = true;<br />

}<br />

private function s<strong>en</strong>dCommand():void<br />

{<br />

var ba:ByteArray = new ByteArray();<br />

ba.writeMultiByte(command.text + "\n", "UTF-8");<br />

telnetCli<strong>en</strong>t.writeBytesToSocket(ba);<br />

command.text = "";<br />

}<br />

The first line of code imports the Telnet class from the custom com.example.programmingas.socket package. The<br />

second line of code declares an instance of the Telnet class, telnetCli<strong>en</strong>t, that is initialized later by the connect()<br />

method. Next, the connect() method is declared and initializes the telnetCli<strong>en</strong>t variable declared earlier. This<br />

method passes the user-specified telnet server name, telnet server port, and a refer<strong>en</strong>ce to a TextArea compon<strong>en</strong>t on<br />

the display list, which is used to display the text responses from the socket server. The final two lines of the connect()<br />

method set the title property for the Panel and <strong>en</strong>able the Panel compon<strong>en</strong>t, which allows the user to s<strong>en</strong>d data to<br />

the remote server. The final method in the main application file, s<strong>en</strong>dCommand(), is used to s<strong>en</strong>d the user's commands<br />

to the remote server as a ByteArray object.<br />

Telnet class overview<br />

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

The Telnet class is responsible for connecting to the remote Telnet server and s<strong>en</strong>ding/receiving data.<br />

The Telnet class declares the following private variables:<br />

The main application file consisting of the user interface for Flex (MXML) or Flash<br />

(FLA).<br />

TelnetSocket.as Docum<strong>en</strong>t class providing the user interface logic (Flash only).<br />

com/example/programmingas3/Telnet/Telnet.as Provides the Telnet cli<strong>en</strong>t functionality for the application, such as connecting to a<br />

remote server, and s<strong>en</strong>ding, receiving, and displaying data.<br />

Last updated 6/6/2012<br />

799

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

Saved successfully!

Ooh no, something went wrong!