User Guide

406 Networking and Communication
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.