Java Reference
In-Depth Information
do {
int i = read();
if (i == -1) disconnect();
// ignore control characters except from cr
else if (i == '\r' || i >= ' ')
buf.append ((char) i);
}
while (!leave && in.available() > 0 && buf.length() <
1024);
show (buf.toString());
buf.setLength (0); // clear buffer
}
}
catch (Exception e) {
if (!leave) show (e.toString() + "\r");
disconnect();
}
}
}
You might have noticed that the method Handler.read() is called for reading data from the stream
instead of just calling in.read() . The only difference is that Handler.read() implements telnet
parameter negotiation, which allows you to use the Terminal sample application as a telnet client by
connecting to port 23 of a corresponding host. The Telnet protocol is widely used to connect computer
systems remotely with a command-line interface. For details of the Telnet protocol please refer
RFC854.
The main application classes, MidpTermial and PdapTerminal , mainly handle the user interface.
The only method that is independent from the user interface is disconnect() , which closes the
connection if a connection exists and notifies the corresponding Handler to terminate.
An important difference of the PDAP implementation when compared to the MIDP implementation is
that the show() method does not manipulate the user interface directly. Because the PDAP AWT is
not thread safe, it is necessary to manipulate the user interface indirectly by calling
invokeAndWait() with an instance implementing the Runnable interface. Here, you use the class
Appender for that purpose. The Appender instance encapsulates the string to be appended to the
list of incoming data. When the AWT calls the run() method of the Appender , AWT has made sure
that it is currently safe to manipulate the user interface. Now, the Appender adds its payload to the
list showing the data sent from the remote end of the connection.
Figure 6.4 shows an example session of the terminal program. Note that the terminal is only a minimal
implementa tion for demonstration purposes. It does not handle any control sequences such as cursor
control, except from carriage return characters ( \r ). Feel free to extend the sample as you like for your
purposes. For example, for applications relying on binary data transfer, it might be better to use a hex
format for sending and receiving data. Listing 6.2 contains the MIDP version of the terminal program,
and Listing 6.3 shows the PDAP version.
Figure 6.4. An example PdapTerminal connection to an HTTP server using the socket
protocol after sending "GET / HTTP 1.0" and an empty MidpTerminal and line.
 
Search WWH ::




Custom Search