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 there are several windowed clients 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 http://www.openssh.com). 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 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,[35] 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.

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 15-10.

Example 15-10. Telnet.java

import java.net.*;
import java.io.*;

/**
 * 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(System.in, s.getOutputStream()).start(  );

        } catch(IOException e) {
            System.out.println(e);
            return;
        }
        System.out.println("Connected OK");
    }
}

/** This class handles one side of the connection. */
/* This class handles one half of a full-duplex connection.
 * Line-at-a-time mode. Streams, not writers, are used.
 */
class Pipe extends Thread {
    DataInputStream is;
    PrintStream os;

    // Constructor
    Pipe(InputStream is, OutputStream os) {
        this.is = new DataInputStream(is);
        this.os = new PrintStream(os);
    }

    // Do something method
    public void run(  ) {
        String line;
        try {
            while ((line = is.readLine(  )) != null) {
                os.print(line);
                os.print("
");
                os.flush(  );
            }
        } catch(IOException e) {
            throw new RuntimeException(e.getMessage(  ));
        }
    }
}


[35] A thread is one of (possibly) many separate flows of control within a single process; see Section 24.2.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.52.188