Solved

Writng a Java Telnet Client

Posted on 2001-08-01
2
751 Views
Last Modified: 2008-02-01
Hi,
   I am now writing a Java Client Telnet program to telnet to other server. I have new a Socket on the server on Port 23 which is the default telnet port. However, when I run the program:

c:>java JavaTelnet xxx.yyy.com

server is : xxx.yyy.com
server socket connected! Socket[addr=xxx.yyy.com/192.168.1.1.,port=23, localport=1212]

then.. the program keep waiting and has no any reply from the server. Can anyone tell me what have I missed? Thanks!


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

public class JavaTelnet {
    public static void main(String[] args) throws IOException {

          //The args should be the server host name
        Socket telnetSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        String host = null;
       
        if (args!=null && args.length>0){
          if (args[0]!=null && !args[0].equals("")){
               host = args[0];
                  try {
                      telnetSocket = new Socket(host, 23);
                      out = new PrintWriter(telnetSocket.getOutputStream(), true);
                      in = new BufferedReader(new InputStreamReader(
                                                  telnetSocket.getInputStream()));
                  } catch (UnknownHostException e) {
                      System.err.println("Don't know about host: " + host + ".");
                      System.exit(1);
                  } catch (IOException e) {
                      System.err.println("Couldn't get I/O for "
                                         + "the connection to: "+ host +".");
                      System.exit(1);
                  }
                  System.out.println("server is : " + host);
                  System.out.println("server socket connected! " + telnetSocket.toString());
                  System.out.println(in.readLine());
                  System.out.println("from server: " + in.readLine());
                 
         
               BufferedReader stdIn = new BufferedReader(
                                             new InputStreamReader(System.in));
               String userInput;
         
               while ((userInput = stdIn.readLine()) != null) {
                   out.println(userInput);
                   System.out.println("telnet: " + in.readLine());
               }
         
               out.close();
               in.close();
               stdIn.close();
               telnetSocket.close();              
          }
        }
    }
}
0
Comment
Question by:john_law70
  • 2
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 30 total points
ID: 6344059
It's not that simple, read the telnet rfc:

http://www.faqs.org/rfcs/rfc854.html

Here's a telnet client with source:

http://www.mud.de/se/jta/


0
 
LVL 92

Expert Comment

by:objects
ID: 6391338
Thanks for the points and best of luck :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
why cannot we forward request once the response is committed 2 46
Arrays.asList  VS  ArrayList 4 71
table example 4 32
how to install java on RHEL image on EC2 4 41
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question