Importorg.apache.commons.net.telnet.TelnetClient;Importjava.io.IOException; Public classTelnetdemo { Public Static voidMain (string[] args)throwsIOException {telnetclient telnet=Newtelnetclient (); String Remoteip= "10.1.1.159"; intRemotePort = 9999; Telnet.connect (Remoteip, RemotePort); System.out.println (Telnet.isavailable ()); System.out.println (telnet.isconnected ()); Ioutil.readwrite (Telnet.getinputstream (), Telnet.getoutputstream (), system.in, System.out); Telnet.disconnect (); System.exit (0); }}
ImportOrg.apache.commons.net.io.Util;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;/*** This was a utility class providing a reader/writer capability required * by the weathertelnet, Rexec, Rshell, and RL Ogin example Programs. * The readWrite * which spawns a reader thread and a writer thread are the only point of the class are to hold the static method. The reader thread * reads from a local input source (presumably stdin) and writes the * data to a remote output Destinati On. The writer thread reads from * a remote input source and writes to a local output destination. * The threads terminate when the remote input source closes. * * */ Public Final classIoutil { Public Static Final voidReadWrite (FinalInputStream Remoteinput,FinalOutputStream Remoteoutput,FinalInputStream Localinput,FinalOutputStream Localoutput) {Thread reader, writer; Reader=NewThread () {@Override Public voidrun () {intch; Try { while(!interrupted () && (ch = localinput.read ())! =-1) {remoteoutput.write (CH); Remoteoutput.flush (); } } Catch(IOException e) {e.printstacktrace (); } } }; Writer=NewThread () {@Override Public voidrun () {Try{util.copystream (remoteinput, localoutput); } Catch(IOException e) {e.printstacktrace (); System.exit (1); } } }; Writer.setpriority (Thread.CurrentThread (). GetPriority ()+ 1); Writer.start (); Reader.setdaemon (true); Reader.start (); Try{writer.join (); Reader.interrupt (); } Catch(interruptedexception e) {e.printstacktrace (); } }}
Commons.net.telnet Use Example