随笔 - 303  文章 - 883  trackbacks - 0
<2007年3月>
25262728123
45678910
11121314151617
18192021222324
25262728293031
1234567

欢迎光临! 
闲聊 QQ:1074961813

随笔分类(357)

我管理的群

公共blog

  • n维空间
  • Email : java3d@126.com 群 : 12999758

参与管理的论坛

好友的blog

我的其他blog

朋友的网站

搜索

  •  

最新评论

old urls : http://www.jscape.com/inetfactory/telnet.html

Code Example

  1 /*
  2  * @(#)TelnetExample.java
  3  *
  4  * Copyright (c) 2001-2002 JScape
  5  * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
  6  * All rights reserved.
  7  *
  8  * This software is the confidential and proprietary information of
  9  * JScape. ("Confidential Information").  You shall not disclose such
 10  * Confidential Information and shall use it only in accordance with
 11  * the terms of the license agreement you entered into with JScape.
 12   */

 13
 14 import  com.jscape.inet.telnet. * ;
 15 import  java.io. * ;
 16
 17 public   class  TelnetExample  extends  TelnetAdapter  {
 18
 19    private  Telnet telnet  =   null ;
 20    private  OutputStream output  =   null ;
 21    private   static  BufferedReader reader  =   null ;
 22    private   boolean  connected  =   false ;
 23
 24    public  TelnetExample(String hostname)  throws  IOException  {
 25
 26     String input  =   null ;
 27      //  create new Telnet instance
 28     telnet  =   new  Telnet(hostname);
 29
 30      //  register this class as TelnetListener
 31     telnet.addTelnetListener( this );
 32
 33      //  establish Telnet connection
 34     telnet.connect();
 35     connected  =   true ;
 36
 37      //  get output stream
 38     output  =  telnet.getOutputStream();
 39
 40      //  sends all data entered at console to Telnet server
 41      while  ((input  =  reader.readLine())  !=   null {
 42        if  (connected)  {
 43         ((TelnetOutputStream) output).println(input);
 44       }
  else   {
 45          break ;
 46       }

 47     }

 48   }

 49
 50    /**  Invoked when Telnet socked is connected.
 51    *  @see  TelnetConnectedEvent
 52    *  @see  Telnet#connect
 53     */

 54    public   void  connected(TelnetConnectedEvent event)  {
 55     System.out.println( " Connected " );
 56   }

 57
 58    /**  
 59    * Invoked when Telnet socket is disconnected. Disconnect can
 60    * occur in many circumstances including IOException during socket read/write.
 61    *  @see  TelnetDisconnectedEvent
 62    *  @see  Telnet#disconnect
 63     */

 64    public   void  disconnected(TelnetDisconnectedEvent event)  {
 65     connected  =   false ;
 66     System.out.print( " Disconnected.  Press enter key to quit. " );
 67   }

 68
 69    /**
 70    * Invoked when Telnet server requests that the Telnet client begin performing specified <code>TelnetOption</code>.
 71    *  @param  event a <code>DoOptionEvent</code>
 72    *  @see  DoOptionEvent
 73    *  @see  TelnetOption
 74     */

 75    public   void  doOption(DoOptionEvent event)  {
 76      //  refuse any options requested by Telnet server
 77     telnet.sendWontOption(event.getOption());
 78   }

 79
 80    /**
 81    * Invoked when Telnet server offers to begin performing specified <code>TelnetOption</code>.
 82    *  @param  event a <code>WillOptionEvent</code>
 83    *  @see  WillOptionEvent
 84    *  @see  TelnetOption
 85     */

 86    public   void  willOption(WillOptionEvent event)  {
 87      //  refuse any options offered by Telnet server
 88     telnet.sendDontOption(event.getOption());
 89   }

 90
 91    /**
 92    * Invoked when data is received from Telnet server.
 93    *  @param  event a <code>TelnetDataReceivedEvent</code>
 94    *  @see  TelnetDataReceivedEvent
 95     */

 96    public   void  dataReceived(TelnetDataReceivedEvent event)  {
 97      //  print data recevied from Telnet server to console
 98     System.out.print(event.getData());
 99   }

100
101    /**
102    * Main method for launching program
103    *  @param  args program arguments
104     */

105    public   static   void  main(String[] args)  {
106      try   {
107       reader  =   new  BufferedReader( new  InputStreamReader(System.in));
108
109        //  prompt user for Telnet server hostname
110       System.out.print( " Enter Telnet server hostname (e.g. 10.0.0.1):  " );
111       String hostname  =  reader.readLine();
112
113        //  create new TelnetExample instance
114       TelnetExample example  =   new  TelnetExample(hostname);
115     }
  catch  (Exception e)  {
116       e.printStackTrace(System.out);
117     }

118   }

119
120 }
 


地震让大伙知道:居安思危,才是生存之道。
posted on 2007-03-25 16:38 小寻 阅读(548) 评论(0)  编辑  收藏 所属分类: j2se/j2ee/j2me

只有注册用户登录后才能发表评论。


网站导航: