日期:2014-05-20  浏览次数:20953 次

如何把阻塞输入流包装成非阻塞的?
使用common-net.jar中的TelnetClient来模拟一个telnet客户端登录和执行命令。
TelnetClient.getInputStream()获取的输入流,利用它的read方法读取时是可能陷入等待。ReadInformation.readTelnet()方法获取同步锁时,如果陷入输入流的read等待,整个程序就无法进行下去了。我想把这个输入流包装成非阻塞的,不知道能不能实现?哪位高手有其它的解决方法给指点一二,在这先谢过了!
代码如下:
Java code

import org.apache.commons.net.telnet.*;   
  
import java.io.*;   
import java.net.SocketException;   
import java.util.StringTokenizer;   
  
public class TmhTelnet implements TelnetNotificationHandler {   
  
    private static TelnetClient tc = null;   
    private static InputStream instr;   
    private static PrintWriter out;
    public static void main(String args[]) throws IOException{ 
        TmhTelnet telnet = new TmhTelnet("127.0.0.1",23);  
        ReadInformation readInfo = new ReadInformation("command.txt",instr,out);
        ReadFileThread rfthread = new ReadFileThread(readInfo);
        ReadTelnetThead rtthread = new ReadTelnetThead(readInfo);
        Thread rtt = new Thread(rtthread);
        rtt.start();
        Thread rft = new Thread(rfthread);
        rft.start();
        System.out.println("return");
        
    }   
    public TmhTelnet(String host, int port) {   
        intconnect(host,port);
    }   
  
    private boolean intconnect(String host, int port) {   
        try {   
            tc = new TelnetClient();   
            TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(   
                    "VT100", false, false, true, false);   
            EchoOptionHandler echoopt = new EchoOptionHandler(true, false,   
                    true, false);   
            SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true,   
                    true, true, true);   
  
            tc.addOptionHandler(ttopt);   
            tc.addOptionHandler(echoopt);   
            tc.addOptionHandler(gaopt); 
            tc.connect(host, port);   
            instr = tc.getInputStream();  
            out = new PrintWriter(tc.getOutputStream());   
            return true;   
        } catch (Exception e) {   
            e.printStackTrace();   
            try {   
                tc.disconnect();   
            } catch (IOException e1) {   
                // TODO Auto-generated catch block   
                e1.printStackTrace();   
            }   
            return false;   
        }          
    }   
       
    public void receivedNegotiation(int negotiation_code, int option_code) {   
        String command = null;   
        if (negotiation_code == TelnetNotificationHandler.RECEIVED_DO) {   
            command = "DO";   
        } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_DONT) {   
            command = "DONT";   
        } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WILL) {   
            command = "WILL";   
        } else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WONT) {   
            command = "WONT";   
        }   
        System.out.println("Received " + command + " for option code "  
                + option_code);   
    }   
} 



Java code

public class ReadFileThread implements Runnable {
    private ReadInformation rf = null;
    public ReadFileThread(ReadInformation rf){
        this.rf = rf;
        
    }

    public void run() {
        while(true){
            rf.readFile();
        }
        
    }
}



Java code

public class ReadTelnetThead implements Runnable{
    private ReadInformation rf = null;
    public ReadTelnetThead(ReadInformation rf){
        this.rf = rf;
        
    }

    public void run() {
        while(true){
            rf.readTelnet();
        }
        
    }
    

}