Example: Building a Telnet client



The Telnet example demonstrates techniques for connecting with a remote server and transmitting data using the Socket class. The example demonstrates the following techniques:

  • Creating a custom telnet client using the Socket class

  • Sending text to the remote server using a ByteArray object

  • Handling received data from a remote server

To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The Telnet application files can be found in the Samples/Telnet folder. The application consists of the following files:

File

Description

TelnetSocket.fla

or

TelnetSocket.mxml

The main application file consisting of the user interface for Flex (MXML) or Flash (FLA).

TelnetSocket.as

Document class providing the user interface logic (Flash only).

com/example/programmingas3/Telnet/Telnet.as

Provides the Telnet client functionality for the application, such as connecting to a remote server, and sending, receiving, and displaying data.

Telnet socket application overview

The main TelnetSocket.mxml file is responsible for creating the user interface (UI) for the entire application.

In addition to the UI, this file also defines two methods, login() and sendCommand(), to connect the user to the specified server.

The following code lists the ActionScript in the main application file:

import com.example.programmingas3.socket.Telnet; 
 
private var telnetClient:Telnet; 
private function connect():void 
{ 
    telnetClient = new Telnet(serverName.text, int(portNumber.text), output); 
    console.title = "Connecting to " + serverName.text + ":" + portNumber.text; 
    console.enabled = true; 
} 
private function sendCommand():void 
{ 
    var ba:ByteArray = new ByteArray(); 
    ba.writeMultiByte(command.text + "\n", "UTF-8"); 
    telnetClient.writeBytesToSocket(ba); 
    command.text = ""; 
}

The first line of code imports the Telnet class from the custom com.example.programmingas.socket package. The second line of code declares an instance of the Telnet class, telnetClient, that will be initialized later by the connect() method. Next, the connect() method is declared and initializes the telnetClient variable declared earlier. This method passes the user-specified telnet server name, telnet server port, and a reference to a TextArea component on the display list, which is used to display the text responses from the socket server. The final two lines of the connect() method set the title property for the Panel and enable the Panel component, which allows the user to send data to the remote server. The final method in the main application file, sendCommand(), is used to send the user's commands to the remote server as a ByteArray object.

Telnet class overview

The Telnet class is responsible for connecting to the remote Telnet server and sending/receiving data.

The Telnet class declares the following private variables:

private var serverURL:String; 
private var portNumber:int; 
private var socket:Socket; 
private var ta:TextArea; 
private var state:int = 0;

The first variable, serverURL, contains the user-specified server address to connect to.

The second variable, portNumber, is the port number that the Telnet server is currently running on. By default, the Telnet service runs on port 23.

The third variable, socket, is a Socket instance that will attempt to connect to the server defined by the serverURL and portNumber variables.

The fourth variable, ta, is a reference to a TextArea component instance on the Stage. This component is used to display responses from the remote Telnet server, or any possible error messages.

The final variable, state, is a numeric value that is used to determine which options your Telnet client supports.

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

The Telnet constructor takes three parameters: server, port, and output. The server and port parameters specify the server name and port number where the Telnet server is running. The final parameter, output, is a reference to a TextArea component instance on the Stage where server output will be displayed to users.

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.

Displaying messages from the socket server

Whenever a message is received from the socket server, or an event occurs, the custom msg() method is called. This method appends a string to the TextArea on the Stage and calls a custom setScroll() method, which causes the TextArea component to scroll to the very bottom. The msg() method is as follows:

private function msg(value:String):void 
{ 
    ta.text += value; 
    setScroll(); 
}

If you didn’t automatically scroll the contents of the TextArea component, users would need to manually drag the scroll bars on the text area to see the latest response from the server.

Scrolling a TextArea component

The setScroll() method contains a single line of ActionScript that scrolls the TextArea component’s contents vertically so the user can see the last line of the returned text. The following snippet shows the setScroll() method:

public function setScroll():void 
{ 
    ta.verticalScrollPosition = ta.maxVerticalScrollPosition; 
}

This method sets the verticalScrollPosition property, which is the line number of the top row of characters that is currently displayed, and sets it to the value of the maxVerticalScrollPosition property.