Solved

Writng a Java Telnet Client

Posted on 2001-08-01
2
732 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json example 39 131
What is JNDI datasource in spring 1 50
jar file executable 12 38
Basic Java Case or If-Else statement... 3 43
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now