public class Telnet{String host;
  int portNum;
  public static void main(String[] args){
  new Telnet().talkTo(args);
  }  private void talkTo(String[] args){
  if(args.length >= 1)
  host = args[0];
  else
  host = "localhost";
  if(args.length >= 2)
  portNum = Integer.parseInt(args[1]);
  else portNum = 23;   System.out.println("Host: " + host + ";port: " + portNum);
  try{
  Socket s = new Socket(host,portNum);   new Pipe(s.getInputStream(),System.out).start();   new Pipe(System.in,s.getOutputStream()).start();
  }
  catch(IOException e){
  System.out.println(e);
  return;
  }
  System.out.println("Connected OK");
  }
}
class Pipe extends Thread{
DataInputStream is;
PrintStream os; Pipe(InputStream is,OutputStream os){
this.is = new DataInputStream(is);
this.os = new PrintStream(os);
} 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());
}
}
}