Recipe 16.9 Program: Telnet Client

This program is a simple Telnet client. Telnet, as you probably know, is the oldest surviving remote login program in use on the Internet. It began on the original ARPAnet and was later translated for the Internet. A Unix command-line client lives on, and several windowed clients are in circulation. For security reasons, the use of Telnet as a means of logging in remotely over the Internet has largely been superseded by SSH (see However, a Telnet client remains a necessity for such purposes as connecting locally, as well as debugging textual socket servers and understanding their protocols. For example, it is common to connect from a Telnet client to an SMTP (email) server; you can often intuit quite a bit about the SMTP server, even if you wouldn't normally type an entire mail session interactively.

When you need to have data copied in both directions at more or less the same time from the keyboard to the remote program, and from the remote program to the screen there are two approaches. Some I/O libraries in C have a function called poll( ) or select( ) that allows you to examine a number of files to see which ones are ready for reading or writing. Java does not support this model. The other model, which works on most platforms and is the norm in Java, is to use two threads,[4] one to handle the data transfer in each direction. That is our plan here; the class Pipe encapsulates one thread and the code for copying data in one direction; two instances are used, one to drive each direction of transfer independently of the other.

[4] A thread is one of (possibly) many separate flows of control within a single process; see Recipe 24.1.

This program allows you to connect to any text-based network service. For example, you can talk to your system's SMTP (simple mail transport protocol) server, or the Daytime server (port 13) used in several earlier recipes in this chapter:

$ java Telnet darian 13 Host darian; port 13 Connected OK Sat Apr 28 14:07:41 2001 ^C $

The source code is shown in Example 16-10.

Example 16-10.
import*; import*; /**  * Telnet - very minimal (no options); connect to given host and service  */ public class Telnet {     String host;     int portNum;     public static void main(String[] argv) {         new Telnet( ).talkTo(argv);     }     private void talkTo(String av[]) {         if (av.length >= 1)             host = av[0];         else             host = "localhost";         if (av.length >= 2)             portNum = Integer.parseInt(av[1]);         else portNum = 23;         System.out.println("Host " + host + "; port " + portNum);         try {             Socket s = new Socket(host, portNum);             // Connect the remote to our stdout             new Pipe(s.getInputStream( ), System.out).start( );             // Connect our stdin to the remote             new Pipe(, s.getOutputStream( )).start( );         } catch(IOException e) {             System.out.println(e);             return;         }         System.out.println("Connected OK");     } } /* This class handles one half of a full-duplex connection.  * Line-at-a-time mode.  */ class Pipe extends Thread {     BufferedReader is;     PrintStream os;     /** Construct a Pipe to read from "is" and write to "os" */     Pipe(InputStream is, OutputStream os) { = new BufferedReader(new InputStreamReader(is));         this.os = new PrintStream(os);     }     /** Do the reading and writing. */     public void run( ) {         String line;         try {             while ((line = is.readLine( )) != null) {                 os.print(line);                 os.print("\r\n");                 os.flush( );             }         } catch(IOException e) {             throw new RuntimeException(e.getMessage( ));         }     } }

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: